iphone – 沿着一系列CGPoints移动图像

iphone – 沿着一系列CGPoints移动图像,第1张

概述我有一个路径存储在一个CGPoints数组中,我想移动一个图像.这是我到目前为止的一般代码: -(void)movePic:(id)sender{ for(int i = 0; i < self.array.count; i++){ CGPoint location = [[self.array objectAtIndex:i] CGPointValue]; 我有一个路径存储在一个CGPoints数组中,我想移动一个图像.这是我到目前为止的一般代码:

-(voID)movePic:(ID)sender{    for(int i = 0; i < self.array.count; i++){        CGPoint location = [[self.array objectAtIndex:i] CGPointValue];        [UIVIEw animateWithDuration:0.1 animations:^{            self.imageVIEw.center = location;        } completion:^(BOol finished){        }];    }}

问题是for循环运行得非常快,所以你只能在最后几点看到动画.我不确定如何更好地设计这个.理想情况下,我可以做些什么来确保一个动画在另一个动画开始之前完成?我不应该使用for循环吗?谢谢

解决方法 您的代码假定UIVIEw动画在主线程中同步运行,而它们没有.

你似乎有两个选择

>显式CAKeyframeAnimation,用于沿任意数量的采样点设置CALayer动画(在它们之间进行插值)
>隐式递归UIVIEw动画,用于沿一系列采样点动画UIVIEw(在它们之间插值)

前者会更有效率 – 我仍然认为我可以向你展示两种选择.

CAKeyframeAnimation

- (voID)movePic:(ID)sender{    //create a mutable core-graphics path    CGMutablePathref path = CGPathCreateMutable();    for(int i = 0; i < self.array.count; i++)    {        CGPoint location = [[self.array objectAtIndex:index] CGPointValue];        CGPathAddlinetoPoint(path,nil,location.x,location.y);    }    //create a new keyframe animation    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];    //add our path to it    pathAnimation.path = path;    //be nice to the system    CGPathRelease(path);    //setup some more animation parameters    pathAnimation.duration = 0.1 * self.array.count;    //add the animation to our imageVIEw's layer (which will start the animation)    [self.imageVIEw.layer addAnimation:pathAnimation forKey:@"pathAnimation"];}

UIVIEw动画

- (voID)movePictopointAtIndex:(unsigned int)index{    //safeguard check...    if ([self.array count] <= index)        return;    //get the next location    CGPoint location = [[self.array objectAtIndex:index] CGPointValue];    //animate the imageVIEw center towards that location    [UIVIEw animateWithDuration:0.1                           delay:0.0                         options:UIVIEwAnimationoptionBeginFromCurrentState | UIVIEwAnimationoptionAllowUserInteraction                     animations:^{        self.imageVIEw.center = location;    } completion:^(BOol finished){        //we are done with that animation,Now go to the next one...        [self movePictopointAtIndex:index+1];    }];}- (voID)movePic:(ID)sender{    [self movePictopointAtIndex:0];}
总结

以上是内存溢出为你收集整理的iphone – 沿着一系列CGPoints移动图像全部内容,希望文章能够帮你解决iphone – 沿着一系列CGPoints移动图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1080631.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存