ios – 在动画制作时更新视图的帧

前端之家收集整理的这篇文章主要介绍了ios – 在动画制作时更新视图的帧前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在做这样的动画:
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与正在设置动画的视图一起使用?

解决方法

这是我执行此操作的代码
#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

我希望这可以帮助你

原文链接:https://www.f2er.com/iOS/335318.html

猜你在找的iOS相关文章