2011-05-24 243 views
1

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

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

任何人都可以引导我吗?

回答

0

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

+0

如果我将使用arc4random按钮,然后旋转会干,我怎么能使其在320 * 480的视图旋转平稳? – Tariq 2011-05-24 10:53:50

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

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

myButton.transform = rotateTransform; 

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

+0

此代码将在同一原点旋转按钮。我想在320 * 480视图中旋转按钮。 – Tariq 2011-05-24 10:52:52

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