在我的iOS应用程序中,我想创建一个UILabel的等效物,可以突出显示,以便简单地引起对自身的注意。所以我希望它在脉动的同时将其颜色从蓝色变为红色。所以我增加然后将fontSize减少20%,因此我还需要更改AnchorPoint,使其看起来从中心向所有方向均匀向外扩展。我已经设法编写实现所有这些目标的代码,只有在我创建对象时执行它。但是,当我通过按下按钮通过IBAction调用它时,colorChange和fontSizeChange无效,而anchorPointChange继续工作。我不知道我做错了什么,虽然我的问题的原因可能很明显,我没有发现它。我尝试了各种代码的实现,子类化UIView和使用CALayer类,但是为了提供一个简洁的例子,我在这里将它全部打包到viewDidLoad中。
任何帮助都将非常感激。
OSX Yosemite版本10.10(14A389),Xcode版本6.1(6A1052d),iOS模拟器版本8.1(550.3)
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) CATextLayer *textLayer;
@end
@implementation ViewController
@synthesize textLayer;
float animationDuration;
- (void)viewDidLoad {
[super viewDidLoad];
textLayer = [CATextLayer layer];
[textLayer setString: @"AB"];
[textLayer setForegroundColor: [UIColor blueColor].CGColor];
[textLayer setFontSize: 50.0];
[textLayer setAlignmentMode: kCAAlignmentCenter];
[textLayer setFrame: CGRectMake(100, 150, 100, 60)];
[textLayer setPosition: CGPointMake(CGRectGetMidX(textLayer.frame), CGRectGetMidY(textLayer.frame))];
[textLayer setAnchorPoint: CGPointMake(0.5, 0.5)];
[[self.view layer] addSublayer:textLayer];
UIButton *activationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
activationButton.backgroundColor = [UIColor lightGrayColor];
activationButton.frame = CGRectMake(130.0, 250.0, 150.0, 30.0);
[activationButton setTitle:@"Highlight Label" forState:UIControlStateNormal];
[activationButton addTarget:self action:@selector(highlightLabel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:activationButton];
animationDuration = 2.0;
// [self changeColor]; // all three methods work perfectly, if called here
// [self changeFontSize: 50.0 scale: 1.2];
// [self changeAnchorPointFrom: CGPointMake(0.5, 0.5) to: CGPointMake(0.5, 0.6)];
/* [self highlightLabel: nil]; */
}
- (IBAction) highlightLabel: (UIButton *) sender {
NSLog(@" textLayer.string: %@", textLayer.string);
[self changeColor]; // Has no effect
[self changeFontSize: 50.0 scale: 1.2]; // Has no effect
[self changeAnchorPointFrom: CGPointMake(0.5, 0.5) to: CGPointMake(0.5, 0.6)]; // Only this method call works
}
- (void)changeFontSize: (float) fontSize scale: (float)factor
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"fontSize"];
animation.duration = animationDuration;
animation.removedOnCompletion = NO;
animation.autoreverses = YES;
animation.fromValue = @(fontSize);
animation.toValue = @(fontSize * factor);
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[textLayer addAnimation:animation forKey:@"fontSizeAnimation"];
}
- (void)changeAnchorPointFrom: (CGPoint) startPoint to: (CGPoint)endPoint
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"anchorPoint"];
animation.duration = animationDuration;
animation.removedOnCompletion = NO;
animation.autoreverses = YES;
animation.fromValue = [NSValue valueWithCGPoint:startPoint];
animation.toValue = [NSValue valueWithCGPoint:endPoint];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[textLayer addAnimation:animation forKey:@"anchorPointAnimation"];
}
- (void)changeColor
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"foregroundColor"];
animation.duration = animationDuration * 2;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;
animation.fromValue = (id)[UIColor blueColor].CGColor;
animation.toValue = (id)[UIColor redColor].CGColor;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[textLayer addAnimation:animation forKey:@"colorAnimation"];
}
@end
答案 0 :(得分:2)
您需要设置文本图层的最终颜色,尝试在highlightLabel
方法的末尾添加此颜色。
[CATransaction begin];
[CATransaction setDisableActions:YES];
[textLayer setForegroundColor: [UIColor redColor].CGColor];
[CATransaction commit];