2014-12-09 3 views
1

내 iOS 앱에서 간략하게주의를 끌 수있는 방식으로 강조 표시 할 수있는 UILabel과 동일한 기능을 만들고 싶습니다. 그래서 나는 펄싱과 동시에 그것의 색깔을 푸른 색에서 붉은 색으로 바꾸기를 원합니다. 그래서 나는 fontSize를 20 %로 늘리거나 줄입니다. 결과적으로 AnchorPoint가 중앙에서 모든 방향으로 똑같이 확장되도록 나타낼 수 있도록 AnchorPoint를 변경해야합니다. 객체를 생성 할 때만이 모든 목표를 달성하는 코드를 작성했습니다. 그러나 버튼을 눌러 IBAction을 통해 호출하면 colorChange 및 fontSizeChange는 아무 효과가 없지만 anchorPointChange는 계속 작동합니다. 나는 내 문제에 대한 이유가 분명히 명백 할 수도 있지만, 내가 잘못하고 있는지 잘 모른다. 나는 그것을 발견하지 못했다. 나는 코드의 다양한 화신을 시도했다. UIView를 서브 클래 싱하고 CALayer 클래스를 사용했지만, 간결한 예제를 제공하기 위해 여기에 viewDidLoad에 모든 것을 패키지화했다.CATextLayer 속성의 CABasicAnimation foregroundColor 및 fontSize가 작동하지 않는 경우가 있음

모든 도움을 주시면 감사하겠습니다.

OSX 요세미티 버전 10.10 (14A389), 엑스 코드 버전 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 

답변

2

당신은 textlayer의 최종 색상을 설정할 필요는이를 추가하려고 highlightLabel 메소드 끝.

[CATransaction begin]; 
[CATransaction setDisableActions:YES]; 
[textLayer setForegroundColor: [UIColor redColor].CGColor]; 
[CATransaction commit]; 
+0

감사합니다. 작동합니다. 문제가 해결되었지만 예상했던 답변이 아니었지만 계속 시도하고 있습니다. 그래서 두 시간 동안 책과 예제를 공부 한 끝에 Nick Lockwood의 iOS Core Animation Advanced Techniques 에서이 코드 조각을 발견했습니다. 그 코드는 이전에 실험했지만 그 중요성을 인식하지 못했기 때문에 똑같은 일을합니다. 나는 여전히 내가 부분적으로 통제 할 수 있다고 느끼고있다. 색상을 찌른 다음 fontSize가 자동으로 작동합니다. 둘 다 같은 스택에 있기 때문입니다. – Steve

+0

저와 함께 책을 나누시겠습니까? 나는 그것을 읽고 싶다. – gabbler

+1

여기 링크를 찾을 수 있습니다 : http://www.charcoaldesign.co.uk/ http://www.informit.com/store/ios-core-animation-advanced-techniques-9780133440751 – Steve