在x轴上创建连续uiimage动画
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在x轴上创建连续uiimage动画相关的知识,希望对你有一定的参考价值。
我想创建像http://disqus.com/继续背景的背景,但我尝试使用这样的代码后无法得到它
continuesBackground =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pattern"]];
continuesBackground.frame=CGRectMake(0, 56, 320, 102);
continuesBackground.layer.zPosition=100;
[UIView animateWithDuration:5.0
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
continuesBackground.transform = CGAffineTransformMakeTranslation(0, 200); }
completion:^(BOOL finish) {
continuesBackground.transform = CGAffineTransformMakeTranslation(200, 0);
}];
怎么做?我需要nstimer吗?
答案
试试这个:
- (void)animateImageViewFromStartPosition:(CGPoint)aStartPosition toEndPosition:(CGPoint)aEndPosition; {
CGRect oldFrame = CGRectMake(aStartPosition.x, aStartPosition.y, continuesBackground.frame.size.width, continuesBackground.frame.size.height);
[continuesBackground setFrame:oldFrame];
[UIView animateWithDuration:5.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^(void){
CGRect newFrame = continuesBackground.frame;
newFrame.origin = aEndPosition;
continuesBackground.frame = newFrame;
}
completion:^(BOOL finished){
if(finished){
[self animateImageViewFromStartPosition:aStartPosition toEndPosition:aEndPosition];
}
}];
}
然后在viewDidLoad
方法中调用该方法:
- (void)viewDidLoad; {
[super viewDidLoad];
[self animateImageViewFromStartPosition:CGPointMake(0.0f, 56.0f) toEndPosition:CGPointMake(320.0f, 56.0f)];
}
您可以轻松地将其修改为变换或任何您想要的变量,它将使动画无限期地保持运行。希望有帮助!
另一答案
- (void)animateImageViewFromStartPosition:(CGPoint)aStartPosition toEndPosition:(CGPoint)aEndPosition; {
CGRect oldFrame = CGRectMake(aStartPosition.x, aStartPosition.y,continuesBackground.frame.size.width, continuesBackground.frame.size.height);
[continuesBackground setFrame:oldFrame];
[UIView animateWithDuration:5.0f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^(void){
CGRect newFrame = continuesBackground.frame;
newFrame.origin = aEndPosition;
continuesBackground.frame = newFrame;
}
completion:^(BOOL finished){
if(finished){
[self animateImageViewFromStartPosition:aStartPosition toEndPosition:aEndPosition];
}
}];
}
然后在viewDidLoad方法中调用该方法:
- (void)viewDidLoad; {
[super viewDidLoad];
[self animateImageViewFromStartPosition:CGPointMake(0.0f, 56.0f) toEndPosition:CGPointMake(320.0f, 56.0f)];
}
在谈论此代码时,请告诉我应用代码的位置
另一答案
以下是Swift 4.2版本中问题的正确答案:
func animateImageViewFrom(startPosition aStartPosition: CGPoint, toEndPosition aEndPosition: CGPoint) {
let oldFrame = CGRect(x: aStartPosition.x, y: aStartPosition.y, width: continuesBackground.frame.size.width, height: continuesBackground.frame.size.height)
continuesBackground.frame = oldFrame
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseInOut, animations: {
var newFrame = self.continuesBackground.frame
newFrame.origin = aEndPosition
self. continuesBackground.frame = newFrame
}) { (finished) in
if finished {
self.animateImageViewFrom(startPosition: aStartPosition, toEndPosition: aEndPosition)
}
}
}
以上是关于在x轴上创建连续uiimage动画的主要内容,如果未能解决你的问题,请参考以下文章