我有一个标签,在我的应用程序中弹出显示点.我使用以下代码使标签的规模变大,然后变小.我还想将颜色从紫色变为绿色.有人能指出我实现这一目标的资源吗?
mLabel.textColor = [UIColor purpleColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
mLabel.transform = CGAffineTransformMakeScale(1.5,1.5);
[UIView setAnimationRepeatCount:1];
mLabel.transform = CGAffineTransformMakeScale(0.5,0.5);
mLabel.textColor = [UIColor greenColor];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud) UIView.animateWithDuration(5, animations: {
myLabel.textColor = UIColor.redColor()
})
Run Code Online (Sandbox Code Playgroud)
标签文字颜色立即改变
我试图为backgroundColorUILabel类的属性设置动画并且到目前为止都没有成功.这是我的代码片段
-(void) blink {
UIColor* originalColor = lblDescription.backgroundColor;
lblDescription.backgroundColor = [UIColor yellowColor];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
lblDescription.backgroundColor = originalColor;
[UIView commitAnimations];
}
//this code works if lblDescription is UIView and does not if UILabel
Run Code Online (Sandbox Code Playgroud)
我发现了一些要求,一些的UILabel属性不是动画,但我无法通过阅读苹果文档证实这种说法.我想知道是否有人可以解释这个问题.
如何使用swift为UILabel的颜色变化设置动画?我见过人们提到使用CALayer,但我无法弄清楚Swift的语法.
这是Objective-C的一个例子
CALayer *layer = myView.layer;
CATextLayer *textLayer = [CATextLayer layer];
[textLayer setString:@"My string"];
[textLayer setForegroundColor:initialColor;
[textLayer setFrame:self.bounds];
[[self.view layer] addSublayer:textLayer];
[UIView animateWithDuration:0.5 animations:^{
textLayer.foregroundColor = finalColor;
}];
Run Code Online (Sandbox Code Playgroud) 我有一个gridView,我用来显示几个GridViewCell : UIView.在GidViewCell增加了一个的UILabel本身并附加一个UITapGestureRecognizer自己.
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self addGestureRecognizer:gestureRecognizer];
[gestureRecognizer release];
Run Code Online (Sandbox Code Playgroud)
在tapped:我想播放一些动画,包括更改标签的背景颜色
- (void) tapped:(id)sender {
[UIView animateWithDuration:1.0
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[(UILabel *)[[self subviews] objectAtIndex:0] setBackgroundColor:[UIColor blueColor]];
}
completion:^(BOOL finished){
[(UILabel *)[[self subviews] objectAtIndex:0] setBackgroundColor:[UIColor whiteColor]];
}
];
[self.delegate didSelectGridViewCell:self];
}
Run Code Online (Sandbox Code Playgroud)
但是标签只是闪烁蓝色一眨眼 - 如果有的话.如果我使用相同的代码,但调整标签的alpha而不是背景颜色,一切都按预期工作.
Apple的文档将backgroundColor列为动画.是吗?难道我做错了什么?
ios ×4
iphone ×3
cocoa-touch ×2
objective-c ×2
swift ×2
animation ×1
calayer ×1
uianimation ×1
uikit ×1
uilabel ×1