动画时更新视图的框架 [英] Update view's frame while it's being animated

查看:14
本文介绍了动画时更新视图的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做这样的动画:

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

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

Works fine, however, this now fails when trying to detect touches on the object that is moving around the screen:

- (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 与动画视图一起工作?

It fails because the view's frame is no longer the same as it's apparent position on the screen when it is being animated. How can I get pointInside to work with a view that is being animated?

推荐答案

这是我的代码

#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

希望对你有帮助

这篇关于动画时更新视图的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆