使用UIView动画时我绝对是初学者。我需要做的就是将imageView从它的起始位置40点向右移动,a位置40指向初始位置的左侧。这应该重复一遍......
- (void)animateLogo
{
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
CGRect frame = CGRectMake(self.Logo.frame.origin.y, 10.0, self.Logo.frame.size.width, self.Logo.frame.size.height);
self.Logo.frame = frame;
}
completion:nil];
}
所有这一切都是向上移动而不是向左移动。我尝试将X参数设置为10但没有变化.... 我该怎么做?
答案 0 :(得分:1)
工作代码:
//note that this rect has x = 0
// you don't have to set this startRect if you created your logo in storyboard
CGRect startRect = CGRectMake(0, 0, 300, 300);
[self.Logo setFrame:startRect];
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
//note that this x is +40
CGRect frame = CGRectMake(self.Logo.frame.origin.x + 40, self.Logo.frame.origin.y, self.Logo.frame.size.width, self.Logo.frame.size.height);
self.Logo.frame = frame;
}
completion:^(BOOL finished) {
}];
<强>更新强>
假设您的徽标是UIImageView
,请尝试以编程方式创建徽标:
CGRect startRect = CGRectMake(318, 47, 133, 97);
UIImageView *LogoImage = [[UIImageView alloc] initWithFrame:startRect];
[LogoImage setImage:[UIImage imageNamed:@"unselected_image.png"]];
[LogoImage setBackgroundColor:[UIColor redColor]];
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
animations:^{
//note that this x is +40
CGRect frame = CGRectMake(LogoImage.frame.origin.x + 40, LogoImage.frame.origin.y, LogoImage.frame.size.width, LogoImage.frame.size.height);
LogoImage.frame = frame;
}
completion:nil];
[self.view addSubview:LogoImage];
答案 1 :(得分:0)
您的代码正在做的是定义一个新的CGRect框架,具有以下属性(x coord,y coord,width,height)
你告诉self.Logo.frame,好的,我希望你在2.0秒内移动到新坐标(宽度/高度相同)。
要让它“做正确的事”,你需要给它正确的x,y坐标。调试此方法的一种方法是NSLog当前的self.logo.frame.origin.x的x,y坐标和y。
答案 2 :(得分:0)
您正在设置y
坐标,您应该在其中x
。 CGRectMake
的定义是,CGRectMake(x,y,width,height)
。将您的代码更改为:
CGRect frame = CGRectMake(10, self.Logo.frame.origin.y,self.Logo.frame.size.width,self.Logo.frame.size.height);
这应该可以正常工作。
答案 3 :(得分:0)
既然你说要重复动画,CABasicAnimation
将是更好的选择。它适用于图层,如果你希望将来停止动画,你将有更好的控制。
如果你采用当前的方法,你将不得不在completion
块内再次调用相同的方法(如果你希望将来停止动画),这有时会使你的应用程序几乎没有响应,这取决于其他进程正在运行。
我不清楚你想要图像视图的动画是哪种方式,但是如果我已经正确地使用它你只想从左到右移动你的图像。如果您有不同持续时间的休息时间,我建议您使用CAKeyFrameAnimation
。我在CABasicAnimation
上附上一些基本代码。
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.autoreverses = YES;
anim.repeatCount = HUGE_VALF;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim.fromValue = [NSValue valueWithCGPoint:self.myImageView.center];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(self.myImageView.center.x + 40.f, self.myImageView.center.y)];
anim.duration = 2.f;
[self.myImageView.layer addAnimation:anim forKey:@"imageViewAnim"];
要详细了解CABasicAnimation
,请参阅Core Animation Programming Guide
希望这个答案可以帮到你。