2011-03-15 56 views
1

我有这个UIView包含两个相同的UIImageViews并置。 我正在寻找使这个视图从右到左翻译,并且当视图到达最后时,立即将其中心重置为默认值以给出连续性的错觉。可可触摸 - 动画的UIView的中心属性

我的问题是,当我在非动画块设置默认中心时,段落是动画的,我看到我的视图'倒带'。

这是我使用的代码:我希望看到我的中心

-(void)drunkenMove{ 

int newCenterX=40; 

BOOL animation=YES; 


CGPoint newCenter; 
if(starView.center.x<=-0){ //so the view has reached the end, being 960*320 
newCenter=CGPointMake(480, starView.center.y); //create a reset center 
    animation=NO; 
}else{ 
newCenter=CGPointMake(starView.center.x-newCenterX, starView.center.y); //else move on 
    animation=YES; 
} 
    if (animation==YES) { 
     [UIView beginAnimations:NULL context:nil]; 
     [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
     [UIView setAnimationDuration:0.3f]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     starView.center=newCenter; 
     [UIView commitAnimations]; 
    }else{ 
     starView.center=newCenter; //here I want to set the center instantly 
     [UIView beginAnimations:NULL context:nil]; 
     [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
     [UIView setAnimationDuration:0.3f]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     starView.center=CGPointMake(480+newCenterX, starView.center.y);; 
     [UIView commitAnimations]; 
    } 

}

复位,然后动画,但我也得到了中心被重置的动画。 任何想法,为什么? 我遇到了与其他UIView动画类似的问题,所以我想我可能错过了一些东西。 顺便说一下,这个功能是由循环线程调用的。

谢谢。

编辑

我的问题在某种程度上通过该方法

[UIView setAnimationBeginsFromCurrentState:YES]; 

从UIView类参考创建: 讨论 使用此方法在iOS 4.0的鼓励及更高版本。相反,您应该使用animateWithDuration:delay:options:animations:completion:方法来指定您的动画和动画选项。

我希望这可以节省一些时间给某人。

回答

0

我使用几乎相同的方法,你和我的作品。也许[UIView setAnimationBeginsFromCurrentState:YES];导致你的问题,但我不知道。

+0

如果它的功能与我制定的相同,请介意发布您的方法?另外,我试图改变这个属性。我的行为略有不同,但我仍然认为“倒带”。 – Lorenzo 2011-03-15 19:50:40

+0

overlay.frame = CGRectMake(100,100,100,100); [UIView beginAnimations:nil context:NULL]; \t \t [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; \t \t [UIView setAnimationDuration:0.25]; \t \t CGRect theFrame = overlay。帧; \t \t [UIView setAnimationDelegate:self]; \t \t \t 如果\t(不可见){ \t \t \t theFrame.origin.x = CGRectGetMaxX(overlay.superview.bounds) - CGRectGetWidth(theFrame); \t \t} \t \t否则{ \t \t \t theFrame.origin.x = CGRectGetMaxX(overlay.superview.bounds); \t \t} \t \t \t \t overlay.frame = theFrame; \t \t [UIView commitAnimations]; – lbrndnr 2011-03-15 20:03:23

+0

哦,我完全删除了你指定的那一行,现在它像一个魅力一样工作。要更好地阅读有关该财产的文档。谢谢! – Lorenzo 2011-03-15 20:21:27

0

我认为您对else子句中的“复位”

starView.center=newCenter; 

是越来越为您创建的下面的动画通过CoreAnimation效率自动包裹起来。刚刚摆脱动画:

}else{ 
    starView.center=newCenter; //here I want to set the center instantly 
    //[UIView beginAnimations:NULL context:nil]; 
    //[UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    //[UIView setAnimationDuration:0.3f]; 
    //[UIView setAnimationBeginsFromCurrentState:YES]; 
    //starView.center=CGPointMake(480+newCenterX, starView.center.y);; 
    //[UIView commitAnimations]; 
} 

和子视图将“跳”你想要的方式。移动动画将在该方法的下一个循环中再次启动。

+0

你是对的,我的问题是我的循环线程每0.3秒调用一次这个方法,所以如果我删除动画,我会在重置和下一个动画之间延迟0.3秒。当然可以做一个解决方法,但我想明白为什么会发生这种情况! – Lorenzo 2011-03-15 20:14:31