在屏幕上随机旋转UIButton

时间:2011-05-24 10:24:20

标签: iphone uibutton core-animation rotation cakeyframeanimation

我想在屏幕上随机旋转按钮。没有定义特定路径,它可以在视图上随机移动。

我不想使用CAKeyframeAnimation。它应该干净简单。

任何人都可以指导我吗?

3 个答案:

答案 0 :(得分:1)

CGAffineTransform cachedTransform = transformedView.transform;
transformedView.transform = CGAffineTransformIdentity;

// Note each of the (untransformed) points of interest.
CGPoint topLeft = CGPointMake(0, 0);
CGPoint bottomLeft = CGPointMake(0, transformedView.frame.size.height);
CGPoint bottomRight = CGPointMake(transformedView.frame.size.width, transformedView.frame.size.height);
CGPoint topRight = CGPointMake(transformedView.frame.size.width, 0);

// Re-apply the transform.
transformedView.transform = cachedTransform;

// Use handy built-in UIView methods to convert the points.
topLeft = [transformedView convertPoint:topLeft toView:parentView];
bottomLeft = [transformedView convertPoint:bottomLeft toView:parentView];
bottomRight = [transformedView convertPoint:bottomRight toView:parentView];
topRight = [transformedView convertPoint:topRight toView:parentView];

另请参阅此链接是否对您有用:Moving UIButton

答案 1 :(得分:0)

使用transform为按钮的CGAffineTransformMakeRotation()属性设置动画,并将其与NSTimer以随机时间间隔合并。

答案 2 :(得分:0)

#define RADIANS(degrees) ((degrees * M_PI) / 180.0)

CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,
     RADIANS(30.0));

myButton.transform = rotateTransform;

使用NSTimer进行此过程并使用arc4random以弧度随机生成角度。