Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

ios - Update view's frame while it's being animated

I'm doing an animation like this:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is my code to do this

#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

I hope this helps you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...