在动画中更新视图的框架

Update view's frame while it's being animated

我正在制作这样的动画:

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.duration = 100.0;
    animation.path = self.animationPath.CGPath;
    [view.layer addAnimation:animation forKey:@"animation"];

工作正常,但是,当尝试检测在屏幕上移动的对象上的触摸时,这现在失败了:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    for (UIView* subview in self.subviews ) {
        if ( [subview hitTest:[self convertPoint:point toView:subview] withEvent:event] != nil ) {
            [self handleTap];
            return YES;
        }
    }
    return NO;
}

它失败了,因为视图的框架不再与其在屏幕上的明显位置相同,当它被动画化时。如何让 pointInside 与正在动画的视图一起工作?

简答:你不能。在 UIView 和 Core Animation 动画中,view/layer 实际上并没有移动,因为它的位置看起来是动画的。相反,有一个“表示层”在移动时绘制动画对象。

如果您想在位置动画“飞行中”时使对象可点击,您必须在跨越整个视图将经过的整个区域的超级视图上放置一个点击手势识别器,然后在动画对象的表示层。

编辑:

我在 Github 上有一个名为 iOS CAAnimation Group demo 的项目,该项目演示了如何在表示层上使用命中测试来检测沿复杂路径动画的视图上的点击。

代码写在Objective-C,但应该还是概念和技术的说明。如果您在理解 Objective-C 代码时遇到困难,请告诉我。我在 Swift 还不错,虽然我大部分时间仍在支付账单 Objective-C,所以这是我最了解的语法。

这是我执行此操作的代码

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet AnimatableView *animableView;
@end

@implementation ViewController

- (void)animateAlongPath
{
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:self.view.frame];

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.duration = 10;
    animation.path = path.CGPath;
    animation.removedOnCompletion = NO;
    animation.fillMode = @"kCAFillModeForwards";
    [self.animableView.layer addAnimation:animation forKey:@"animation"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)animate:(id)sender {
    [self animateAlongPath];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];

    CALayer * selectedlayer = (CALayer*)[self.animableView.layer.presentationLayer hitTest:location];
    if(selectedlayer != nil)
        NSLog(@"touched");
    else
        NSLog(@"dont touched");
}


@end

希望对你有帮助