제목을 업데이트 할 때 UIButton이 깜박이는 것을 방지하는 방법
UIButton에서 setTitle을 호출하면 iOS 7에서 버튼이 깜박입니다. myButton.highlighted = NO 설정을 시도했지만 버튼 깜박임이 중지되지는 않았습니다.
[myButton setTitle:[[NSUserDefaults standardUserDefaults] stringForKey:@"elapsedLabelKey"] forState:UIControlStateNormal];
myButton.highlighted = NO;
제목을 업데이트 한 타이머를 설정하는 방법은 다음과 같습니다.
- (void)actionTimer {
if (myTimer == nil) {
myTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: @selector(showActivity)
userInfo: nil
repeats: YES];
}
}
실제로 제목을 업데이트하는 방법은 다음과 같습니다.
- (void)showActivity {
NSString *sym = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencySymbol];
if (pauseInterval == nil) {
// Update clock
seconds = [[NSDate date] timeIntervalSinceDate:startInterval] - breakTime;
// Update total earned
secRate = rate.value / 60 / 60;
total = secRate * seconds;
[totalLabel setTitle:[NSString stringWithFormat:@"%@%.4f",sym,total] forState:UIControlStateNormal];
days = seconds / (60 * 60 * 24);
seconds -= days * (60 * 60 * 24);
int hours = seconds / (60 * 60);
fhours = (float)seconds / (60.0 * 60.0);
seconds -= hours * (60 * 60);
int minutes = seconds / 60;
seconds -= minutes * 60;
// Update the timer clock
[elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal];
}
}
버튼 유형을 UIButtonTypeCustom으로 설정하면 깜박임이 중지됩니다.
[UIView setAnimationsEnabled:NO]
다른 애니메이션에 영향을 미칠 수있는 것보다 더 나은 방법 은 특정 타이틀 애니메이션 만 비활성화하는 것입니다.
목표 -C :
[UIView performWithoutAnimation:^{
[myButton setTitle:text forState:UIControlStateNormal];
[myButton layoutIfNeeded];
}];
빠른:
UIView.performWithoutAnimation {
myButton.setTitle(text, for: .normal)
myButton.layoutIfNeeded()
}
* 참고 *
_button의 " buttonType "이 "UIButtonTypeSystem"인 경우 아래 코드는 유효하지 않습니다 .
[UIView setAnimationsEnabled:NO];
[_button setTitle:@"title" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
_button의 " buttonType "이 "UIButtonTypeCustom" 이면 위 코드가 유효합니다 .
제목 변경시 원치 않는 UIButton 애니메이션을 중지하는 방법 의 응답을 참조하십시오 . :
[UIView setAnimationsEnabled:NO];
[elapsed setTitle:[NSString stringWithFormat:@"%.2i:%.2i:%.2i:%.2i",days,hours,minutes,seconds] forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
기본 "setTitle" 동작은 확실히 싫어요!
내 솔루션은 다음과 같습니다.
[UIView setAnimationsEnabled:NO];
[_button layoutIfNeeded];
[_button setTitle:[NSString stringWithFormat:@"what" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
또한 스토리 보드의 버튼 속성에서 다음을 선택 취소합니다.
- 하이라이트에서 반전
- 하이라이트 조정 이미지
그리고 확인 :
- 비활성화 된 이미지 조정
iOS 8 에서도 테스트 및 작업했습니다 .
업데이트-Swift
사용자 지정 단추를 만들고 setTitle 메서드를 재정의하는 것이 좋습니다 .
class UnflashingButton: UIButton {
override func setTitle(title: String?, forState state: UIControlState) {
UIView.setAnimationsEnabled(false)
self.layoutIfNeeded()
super.setTitle(title, forState: state)
UIView.setAnimationsEnabled(true)
}
}
나중에 쉽게 사용할 수 있도록 UIButton에 대한 다른 함수를 간단히 만들 수 있습니다.
extension UIButton {
func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) {
UIView.performWithoutAnimation {
self.setTitle(title, for: controlState)
self.layoutIfNeeded()
}
}
}
그게 다야!
And just set the title as you would before but replace setTitle
with setTitleWithoutAnimation
Try this, this works in newer versions of IOS:
class CustomButtonWithNoEffect : UIButton {
override func setTitle(_ title: String?, for state: UIControlState) {
UIView.performWithoutAnimation {
super.setTitle(title, for: state)
super.layoutIfNeeded()
}
}
}
I'm new to coding and Stackoverflow, so don't have enough reputation to comment directly to expand on https://stackoverflow.com/users/4673064/daniel-tseng excellent answer. So I have to write my own new answer, and it is this:
extension UIButton {
func setTitleWithoutAnimation(_ title: String?, for controlState: UIControlState) {
UIView.performWithoutAnimation {
self.setTitle(title, for: controlState)
self.layoutIfNeeded()
}
}
}
Works great, EXCEPT:
If all my calls later on in the code to "setTitleWithoutAnimation" do not specify a sender, then I get these weird messages related to CoreMedia or CoreData, e.g., "Failed to inherit CoreMedia permissions from 2526: (null)"
This is probably pretty basic to coding and iOS, but for me as a new coder, it sent me on a rabbit trail for a while, as in: Today Extension Failed to inherit CoreMedia permissions from, where people had interesting answers but that did NOT reach the root of my problem.
So I finally found that I had to go through and give my parameter-less functions parameters, i.e., I had to specify a sender. That sender could be UIButton, Any, or AnyObject. All of those worked, but ultimately there appears to be a conflict between adding a button extension with "self" and not specifically saying later on that a button is using it.
Again, probably basic, but new to me, so I figured it would be worth sharing.
Another trick
@IBOutlet private weak var button: UIButton! {
didSet {
button.setTitle(title, for: .normal)
}
}
This works only if you know the value of the title without making any API calls. The button's title will be set before the view is loaded so you won't see the animation
'program tip' 카테고리의 다른 글
Xcode 4 코드 감지가 작동하지 않습니다. (0) | 2020.10.13 |
---|---|
NSUserDefaults에 이미지를 저장 하시겠습니까? (0) | 2020.10.13 |
kubernetes 포드에서 이미지 가져 오기를 다시 시도하는 방법은 무엇입니까? (0) | 2020.10.13 |
Atom 메뉴가 없습니다. (0) | 2020.10.13 |
텍스트 파일에서 첫 번째 줄을 읽는 Windows 배치 명령 (0) | 2020.10.13 |