У меня мало опыта в Objective-C. Я пытаюсь получить конец анимации после того, как пользователь нажал кнопку. В viewDidLoad
я добавил это:Получить CompletionBlock для CABasicAnimation
UILongPressGestureRecognizer *recoginzer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onPress:)];
[_buttonStart addGestureRecognizer:recoginzer];
затем в методе я использовал setCompletionBlock
, чтобы определить, когда анимация закончена, но она не работает.
-(void)onPress:(UILongPressGestureRecognizer*)longpress {
if (longpress.state == UIGestureRecognizerStateBegan) {
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(2, 2, _buttonStart.frame.size.width-4, _buttonStart.frame.size.height-4) cornerRadius:(_buttonStart.frame.size.width/2)-8].CGPath;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor whiteColor].CGColor;
circle.lineWidth = 2.5;
[_buttonStart.layer addSublayer:circle];
[CATransaction begin];
CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration = 3.0;
drawAnimation.repeatCount = 1.0;
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue = [NSNumber numberWithFloat:1.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
[CATransaction setCompletionBlock:^{
NSLog(@"DONE");
}];
[CATransaction commit];
} else if (longpress.state == UIGestureRecognizerStateEnded || longpress.state == UIGestureRecognizerStateCancelled || longpress.state == UIGestureRecognizerStateFailed) {
[circle removeFromSuperlayer];
NSLog(@"long press OUT");
}
}
заранее спасибо за вашу поддержку
Спасибо! Для того, чтобы не вызывать блок в UIGestureRecognizerStateEnded, UIGestureRecognizerStateCancelled и UIGestureRecognizerState не удалось. Когда анимация не завершена [circle removeFromSuperlayer]; Как я могу сделать? – Joannes