ios - 在动画中更新 View 的框架

标签 ios uiview core-animation caanimation cakeyframeanimation

我正在做这样的动画:

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;
}

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

最佳答案

这是我的代码

#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

希望对你有帮助

关于ios - 在动画中更新 View 的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37818440/

相关文章:

iphone - 如何让 CAKeyframeAnimation 在覆盖另一个 CAKeyframeAnimation 时获取当前状态?

iphone - 3D 立方体问题,第 2 部分

ios - Firebase 存储 imageReference.delete{ (error) in } 方法导致我的应用程序崩溃,代码为 "signal SIGABRT"

ios - 从 UITableView 转发 ControlEvent

ios - 偏移 UIView 不会移动所有 View 元素

ios - 如何使可重用 View 接受泛型类型

ios - 如何变换 uilabel 保持其 x 位置相同并缩放以减小字体大小

javascript - 如何在 NativeBase 中使用 DeckSwiper 元素而不会出现 "Element type is invalid"错误?

iOS 图表框架 maximumFractionDigits 不工作

ios - 将 subview 添加到另一个 subview ios