我想无限期地旋转图像视图360度.
UIView.animate(withDuration: 2, delay: 0, options: [.repeat], animations: {
self.view.transform = CGAffineTransform(rotationAngle: 6.28318530717959)
}, completion: nil)
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我一直在尝试UIButton使用以下代码运行旋转360度的动画:
UIView.animateWithDuration(3.0, animations: {
self.vineTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2))
self.instagramTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2))
})
Run Code Online (Sandbox Code Playgroud)
但是,它不会旋转360度,因为UIButton它已经在该位置.
如何旋转我的UIButton 360度?
我试图UIView围绕它的中心旋转一些,所以简单的代码就像(伪代码):
[UIView beginAnimations:@"crazyRotate" context:nil];
[UIView setAnimationDuration:1.0];
someview.transform = CGAffineTransformMakeRotation(angle);
[UIView commitAnimations]
Run Code Online (Sandbox Code Playgroud)
现在,如果我设置角度来说M_PI/2,那么事情就会很好地旋转.如果我把它设置为2*M_PI,它确实"没有".我可以理解,矩阵转化为无效的东西(旋转360意味着"停留"在某种意义上),但是,我想要旋转它5次(想想一个报纸旋转刻度即将到来 - 我不是非常善于描述,希望有人理解).所以,我尝试将设置角度添加到180度(M_PI)并添加嵌套animatationBlock.但我想,因为我someview.transition再次设置相同的属性(),它会以某种方式忽略它.我尝试用角度M_PI将动画的重复计数设置为2,但它似乎只是旋转180,回到直线位置,然后再次启动旋转.
所以,我有点想法,任何帮助赞赏!--t
我将钟表臂指向12点钟到当前时间.如果是,11点,我想让手臂顺时针旋转到11点位置.但当然如果我使用:
CGAffineTransform rotation = CGAffineTransformMakeRotation(2*M_PI*11/12);
[UIView animateWithDuration:3.0
animations:^{
clockArm.transform = rotation;
}];
Run Code Online (Sandbox Code Playgroud)
旋转逆时针旋转.我试过了:
CGFloat angle = 2*M_PI*11/12;
CGAffineTransform firstRotation = CGAffineTransformMakeRotation(M_PI-0.001);
CGFloat firstRotationTime = 3.0*(M_PI/angle);
CGAffineTransform secondRotation = CGAffineTransformMakeRotation(angle);
CGFloat secondRotationTime = 3.0 - firstRotationTime;
[UIView animateWithDuration:firstRotationTime
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
self.clockArm1.transform = firstRotation;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:secondRotationTime
delay:0.0
options:UIViewAnimationCurveLinear
animations:^{
self.clockArm1.transform = secondRotation;
}
completion:^(BOOL finished){
}];
}];
Run Code Online (Sandbox Code Playgroud)
动画确实是顺时针方向,但它很不稳定 - 动画似乎仍在为第一个动画做一个UIViewAnimationEaseInOut.我做错了什么,还是有另一种方法可以实现我的目标?
我有一个.png格式的轮子图像,我想知道我怎么能动画,以便它连续旋转,我搜索stackoverflow并找到某些代码片段,帮助我旋转我的图像,但它不会连续旋转,它会只需旋转几秒钟即可停止,代码如下
viewdidload中的代码
UIImageView *imageToMove =
[[UIImageView alloc] initWithImage:[UIImageimageNamed:@"horo_circle.png"]];
[self.view addSubview:imageToMove];
[self rotateImage:imageToMove duration:5.0
curve:UIViewAnimationCurveEaseIn degrees:180];
Run Code Online (Sandbox Code Playgroud)
和动画
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve degrees:(CGFloat)degrees
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform =
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)
以及导入后的以下行
#define M_PI 3.14159265358979323846264338327950288 /* pi */
#define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI)
Run Code Online (Sandbox Code Playgroud) 我有2个按钮开始和停止.如果我单击开始按钮,它会在我单击停止按钮时启动它的旋转,它会在单击停止按钮时停在确切位置.
再次点击开始按钮,它开始从前一状态开始旋转.
我是如何实现这一目标的?
在开始我写这个代码
CABasicAnimation *rotation=[CABasicAnimationanimationWithKeyPath:@"transform.rotation"];
rotation.fromValue=[NSNumber numberWithFloat:0];
rotation.toValue=[NSNumber numberWithFloat:((360*M_PI)/180)];
rotation.duration=2;
rotation.repeatCounts=INFINITY;
[image.layer addAnimation:rotation forKey:@"Spin"];
Run Code Online (Sandbox Code Playgroud) 我有一个图像,我想旋转360°(顺时针),然后重复,我无法按正确的方式进行此操作.我可以做这个:
UIView.Animate(1, 0, UIViewAnimationOptions.Repeat,
() =>
{
LoadingImage.Transform = MonoTouch.CoreGraphics.CGAffineTransform.MakeRotation(-(float)(Math.PI));
},
() =>
{
});
Run Code Online (Sandbox Code Playgroud)
这将我的图像旋转180°然后快速回到开始并重复.所以,我想如果我做了像1.99*PI这样的东西,它几乎会一直旋转,看起来还不错.不幸的是,动画系统比这更聪明,而只是反向旋转!
那么让图像连续旋转360°的正确方法是什么?
更新
在得到帮助或Eric的评论后,我得到了这个:
CABasicAnimation rotationAnimation = new CABasicAnimation();
rotationAnimation.KeyPath = "transform.rotation.z";
rotationAnimation.To = new NSNumber(Math.PI * 2);
rotationAnimation.Duration = 1;
rotationAnimation.Cumulative = true;
rotationAnimation.RepeatCount = 10;
LoadingImage.Layer.AddAnimation(rotationAnimation, "rotationAnimation");
Run Code Online (Sandbox Code Playgroud)
在我的例子中,它旋转360°,10次旋转(参见参考资料RepeatCount),但我没有看到一种方法告诉它重复,直到我停止它.CABasicAnimation有一个RepeatCount和一个RepeatDuration,但似乎没有一个属性告诉它只是不断重复.我可以设置RepeatCount一些适当的高值,它会一直旋转,直到用户可能失去兴趣,但这似乎不是一个很好的解决方案.
我有一个动画,它应该不断旋转图像。但是它有几个问题。速度很奇怪,尽管我将它设置为不断重复,但您可以看到它是如何开始、停止然后重复的。这不应该发生。它应该是不间断的旋转。此外,另一个问题是当动画停止时,图像由于某种原因向左移动。
这是我的代码:
func animateLogo()
{
UIView.animate(withDuration: 6.0, delay: 0.0, options: .repeat, animations: {
self.logo.transform = CGAffineTransform(rotationAngle: ((180.0 * CGFloat(Double.pi)) / 180.0))
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) ios ×8
iphone ×3
objective-c ×3
swift ×3
uiview ×2
caanimation ×1
cocoa-touch ×1
uibutton ×1
uiimage ×1
uiimageview ×1
xamarin ×1