UILabel의 배경색에 애니메이션을 적용하는 방법은 무엇입니까?
작동하는 것처럼 보이지만 작동하지 않습니다. 색상이 즉시 녹색으로 바뀝니다.
self.labelCorrection.backgroundColor = [UIColor whiteColor];
[UIView animateWithDuration:2.0 animations:^{
self.labelCorrection.backgroundColor = [UIColor greenColor];
}];
어디서나 문서화 된 것을 찾을 수 는 없지만 코드가 vanilla에서 잘 작동하기 때문에 의 backgroundColor속성이 UILabel애니메이션 불가능한 것처럼 보입니다 UIView. 이 해킹은 레이블보기 자체의 배경색을 설정하지 않는 한 작동하는 것처럼 보입니다.
#import <QuartzCore/QuartzCore.h>
...
theLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
[UIView animateWithDuration:2.0 animations:^{
theLabel.layer.backgroundColor = [UIColor greenColor].CGColor;
} completion:NULL];
빠른
( 중요 ) UILabel의 배경색을 지우도록 설정합니다 (IB 또는 코드에서). 예를 들면 :
override func viewDidLoad() { super.viewDidLoad() myLabel.backgroundColor = UIColor.clear }레이어 배경색에 애니메이션을 적용합니다.
@IBAction func animateButtonTapped(sender: UIButton) { UIView.animate(withDuration: 1.0, animations: { self.myLabel.layer.backgroundColor = UIColor.red.cgColor }) }
주 CGColor사후이 추가됩니다 UIColor.
결과
애니메이션을 적용 할 수 있지만 어떤 이유로 든 먼저 (프로그래밍 방식으로) clearColor로 설정해야했습니다. 그것 없이는 애니메이션이 작동하지 않거나 보이지 않았습니다.
사용자 정의 테이블 셀에서 UILabel의 배경색을 애니메이션화하고 있습니다. 다음은 willDisplayCell 메서드의 코드입니다. 많은 레이아웃이 테이블에 의해 수정되기 때문에 cellForRow에서 애니메이션을 설정하지 않을 것입니다.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
((RouteListCell *)cell).name.backgroundColor = [UIColor clearColor];
[((RouteListCell *)cell).name backgroundGlowAnimationFromColor:[UIColor whiteColor] toColor:[UIColor redColor] clearAnimationsFirst:YES];
}
내 애니메이션 루틴은 다음과 같습니다.
-(void) backgroundGlowAnimationFromColor:(UIColor *)startColor toColor:(UIColor *)destColor clearAnimationsFirst:(BOOL)reset;
{
if (reset)
{
[self.layer removeAllAnimations];
}
CABasicAnimation *anAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
anAnimation.duration = 1.00;
anAnimation.repeatCount = HUGE_VAL;
anAnimation.autoreverses = YES;
anAnimation.fromValue = (id) startColor.CGColor; // [NSNumber numberWithFloat:1.0];
anAnimation.toValue = (id) destColor.CGColor; //[NSNumber numberWithFloat:0.10];
[self.layer addAnimation:anAnimation forKey:@"backgroundColor"];
}
스위프트 3
레이블 배경색을 흰색에서 녹색으로 애니메이션하려면 다음과 같이 레이블을 설정하십시오.
self.labelCorrection.backgroundColor = UIColor.clear
self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
다음과 같이 애니메이션 :
UIView.animate(withDuration: 2.0) {
self.labelCorrection.layer.backgroundColor = UIColor.green.cgColor
}
다시 애니메이션을 적용 할 수 있도록 원래 상태로 돌아가려면 애니메이션을 제거해야합니다.
self.labelCorrection.layer.backgroundColor = UIColor.white.cgColor
self.labelCorrection.layer.removeAllAnimations()
NSTimer 또는 CADisplayLink 콜백을 기반으로 적절한 프레임 속도 (예 : 20fps)로 색상 애니메이션을 수동으로 수행합니다. 전체 애니메이션 시간 (예 : 2.0 초)에 대한 분수 경과 시간을 기반으로 RGB 또는 HSV로 고유 한 색상 변경 곡선을 계산하거나 40 개의 중간 색상 배열을 사용해야합니다.
이것을 시도하십시오,
[UIView transitionWithView:yourView duration:0.2 options:UIViewAnimationOptionTransitionNone animations:^{
[yourView setBackgroundColor:[UIColor colorWithRed:0.8588 green:0.8588 blue:0.8588 alpha:1]];
}completion:^(BOOL finished) {
}];
그것은 나를 위해 작동합니다.
I recently came across this problem once again and after some research I figured that basically nothing changed (and probably won't in foreseeable future), so I ended up using Facebook POP framework (not only) for that.
You can easily do animations of basic properties on both view and layer, but in addition, it provides smooth transition between colors (and basically whatever you want, even your custom types, with some additional coding). So for background color, you would do something like this:
// Create spring animation (there are more types, if you want)
let animation = POPSpringAnimation(propertyNamed: kPOPViewBackgroundColor)
// Configure it properly
animation.autoreverses = false
animation.removedOnCompletion = true
animation.fromValue = UIColor.yourBeginningColor()
animation.toValue = UIColor.yourFinalColor()
// Add new animation to your view - animation key can be whatever you like, serves as reference
label.pop_addAnimation(animation, forKey: "YourAnimationKey")
Viola, now you can animate background colors on everything that has that property!
it says in the docs that backgroundColor is animatable
Don't really know since when.
If you don't want to dip down into Quartz, per answer one of the prev answers, create an empty UIView of same size as UILabel and position it in same spot as the UILabel (and in Z order, under it) and you can animate that UIView's background color (I've tried this, and it works for me).
Swift 4.1 UILabel Extension:
Add this extension to your code:
extension UILabel {
/** Animates the background color of a UILabel to a new color. */
func animateBackgroundColor(to color : UIColor?, withDuration : TimeInterval, completion : ((Bool) -> Void)? = nil) {
guard let newColor = color else { return }
self.backgroundColor = .clear
UIView.animate(withDuration: withDuration, animations: {
self.layer.backgroundColor = newColor.cgColor
}, completion: completion)
}
}
You should use it like this:
myUILabel.animateBackgroundColor(to: .red, withDuration: 0.5)
Now, if you want to restore the original color, use it like this:
// Storing the orignal color:
let originalColor = myUILabel.backgroundColor
// Changing the background color:
myUILabel.animateBackgroundColor(to: .red, withDuration: 0.5)
// ... Later:
// Restoring the original color:
myUILabel.animateBackgroundColor(to: originalColor, withDuration: 0.5, completion: {
(value: Bool) in
myUILabel.backgroundColor = originalColor
})
Here is the reference:
animations A block object containing the changes to commit to the views. This is where you programmatically change any animatable properties of the views in your view hierarchy. This block takes no parameters and has no return value. This parameter must not be NULL
내가 이해하는 것은 뷰가 애니메이션 블록을 호출 할 때 그 이후로 뷰 레이블 배경색이 녹색으로 커밋된다는 것입니다. 그런 다음 레이블 배경색을 다른 것으로 변경하지 않으면 항상 녹색이됩니다. 이것은 내가 추측하는 것입니다
참고 URL : https://stackoverflow.com/questions/3762561/how-to-animate-the-background-color-of-a-uilabel
'IT TIP' 카테고리의 다른 글
| 연관 배열을 PHP에서 값의 간단한 배열로 변환 (0) | 2020.10.25 |
|---|---|
| Ajax에서 GET 대 POST (0) | 2020.10.25 |
| int 배열에서 요소의 인덱스를 찾는 방법은 무엇입니까? (0) | 2020.10.25 |
| cat의 출력을 cURL로 파이프하여 파일 목록을 다운로드합니다. (0) | 2020.10.25 |
| .NET 경로가 디렉토리가 아닌 파일인지 확인하는 방법은 무엇입니까? (0) | 2020.10.25 |
