我想反复移动我的UIView从左到右,直到右侧边界没有到达视野。下面的代码重复运行,但只移动5个点,从起点开始重复,我想从上一个当前状态重复,+ 5再次移动。
CGPoint pos = mover.center;
pos.x=25;
int count = 25;
[UIView beginAnimations:nil context:NULL];
pos.x=count;
[UIView setAnimationDuration:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatCount:INFINITY];
//[UIView setAnimationRepeatAutoreverses:YES];
// CGPoint pos = mover.center;
//pos.x +=5;
count = (pos.x+=5);
mover.center = pos;
我希望看到物体在+5点间隔内移动。 而且我是目标c和iphone的新手,所以请尽快帮助我。 基本上我的任务是屏幕边框上的移动视图(从左到右,从上到下,从右到左,从下到上)意味着按顺序移动视图并反复通过模拟器屏幕的边框。 我希望你的答案对我和其他新技术(iPhone)有帮助。 非常感谢你。
答案 0 :(得分:1)
要将视图从一个位置移动到另一个位置,我会将其框架设置在动画块内。这样的事情会对你有所帮助:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// `customView` is a UIView defined in .h file
customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
customView.backgroundColor = [UIColor redColor];
[self.view addSubview:customView];
[self rotateViewInSelfBoundary];
}
-(void)rotateViewInSelfBoundary
{
CGRect screen = [[UIScreen mainScreen] bounds];
// Top Left to top right
[UIView animateWithDuration:3.0f
animations:^{
[customView setFrame:CGRectMake(screen.size.width - customView.frame.size.width, 0, 100, 100)];
}
completion:^(BOOL finished)
{
// Top right to bottom right
[UIView animateWithDuration:3.0f
animations:^{
[customView setFrame:CGRectMake(screen.size.width - customView.frame.size.width, screen.size.height - customView.frame.size.height, 100, 100)];
}
completion:^(BOOL finished)
{
// bottom right to bottom left
[UIView animateWithDuration:3.0f
animations:^{
[customView setFrame:CGRectMake(0, screen.size.height - customView.frame.size.height, 100, 100)];
}
completion:^(BOOL finished)
{
// bottom left to top left
[UIView animateWithDuration:3.0f
animations:^{
[customView setFrame:CGRectMake(0, 0, 100, 100)];
}
completion:^(BOOL finished)
{
// call the same function again.
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(rotateViewInSelfBoundary) object:nil];
[self performSelector:@selector(rotateViewInSelfBoundary) withObject:nil afterDelay:0.0f];
}];
}];
}];
}];
}
答案 1 :(得分:0)
使用 UIViewAnimationOptionBeginFromCurrentState 赞
[UIView animateWithDuration:1.0f delay:2.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionBeginFromCurrentState animations:^{ //your animation } completion:nil];
答案 2 :(得分:0)
如果要在正方形中移动视图,请定义它应在其间设置的四个CGPoint
值,然后:
NSArray *values = @[[NSValue valueWithCGPoint:point0],
[NSValue valueWithCGPoint:point1],
[NSValue valueWithCGPoint:point2],
[NSValue valueWithCGPoint:point3],
[NSValue valueWithCGPoint:point0]];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.values = values;
animation.duration = 5.0;
animation.repeatCount = HUGE_VALF;
[mover.layer addAnimation:animation forKey:@"moveAroundBorder"];
这将处理视图的所有移动,而无需您进一步干预。
如果您想使用基于iOS 7块的关键帧动画,那么它将是:
[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
[UIView animateKeyframesWithDuration:5.0 delay:0.0 options:UIViewKeyframeAnimationOptionRepeat | UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime:0.00 relativeDuration:0.25 animations:^{
mover.center = point0;
}];
[UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
mover.center = point1;
}];
[UIView addKeyframeWithRelativeStartTime:0.50 relativeDuration:0.25 animations:^{
mover.center = point2;
}];
[UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
mover.center = point3;
}];
} completion:nil];
} completion:nil];
请注意animateKeyframesWithDuration
中animateWithDuration
的好奇嵌套。那是因为UIViewKeyframeAnimationOptionCalculationModeLinear
奇怪地没有停止“缓入/缓出”行为,所以你必须在外部动画块上指定UIViewAnimationOptionCurveLinear
。