gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:20:31 24 4
gpt4 key购买 nike

我正在做这样的动画:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com