【发布时间】:2014-09-10 11:02:36
【问题描述】:
我正在尝试将UIView 动画化为浮动对象或气球:D
UIView 在屏幕中间,我希望它在第一个启动点周围随机浮动。不跨越屏幕或类似的东西,只是漂浮在它周围 5 个像素的区域内。
有什么建议吗? :D
我试过这个:
[UIView animateWithDuration:0.1
delay:0.0
options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction
animations:^{
myCircleUIView.transform = CGAffineTransformMakeTranslation([self randomFloatingGenerator], [self randomFloatingGenerator]);
}
completion:NULL];
randomFloatingGenerator 生成一个介于 -5 和 5 之间的数字。问题是,它只执行一次,并且不断重复使用相同的随机值。
EDIT1: 现在我已经尝试过了
-(void)animationLoop{
[UIView animateWithDuration:1.0
animations: ^{ myCircleUIView.transform = CGAffineTransformMakeTranslation([self randomFloatingGenerator], [self randomFloatingGenerator]); }
completion:
^(BOOL finished) {
[UIView animateWithDuration:1.0
animations:^{ myCircleUIView.transform = CGAffineTransformMakeTranslation(0,0);
}
completion:
^(BOOL finished) {[self animationLoop];}];
}];
但它仍然无法正常工作,动画......糟糕......我想我正在犯一些愚蠢的错误,我需要第二双眼睛来帮助我。
EDIT2: 修好了。
-(void)animationLoop{
CGPoint oldPoint = CGPointMake(myCircleUIView.frame.origin.x, myCircleUIView.frame.origin.y);
[UIView animateWithDuration:1.0
animations: ^{ myCircleUIView.frame = CGRectMake(myCircleUIView.frame.origin.x + [self randomFloatingGenerator], myCircleUIView.frame.origin.y + [self randomFloatingGenerator], myCircleUIView.frame.size.width, myCircleUIView.frame.size.height); }
completion:
^(BOOL finished) {
[UIView animateWithDuration:1.0
animations:^{ myCircleUIView.frame = CGRectMake(oldPoint.x, oldPoint.y, myCircleUIView.frame.size.width, myCircleUIView.frame.size.height);}
completion:
^(BOOL finished) {[self animationLoop];}];
}];
}
感谢任何人的帮助。
编辑 3:
有人发布了增强的代码。
【问题讨论】:
-
使用 NSTimer 并在其中调用上面的代码...
标签: ios objective-c animation uiview