在屏幕上随机旋转UIButton
问题描述:
答
使用CGAffineTransformMakeRotation()
为按钮的transform
属性设置动画效果,并将其与NSTimer
以随机时间间隔相结合。
答
#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
答
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
如果我将使用arc4random按钮,然后旋转会干,我怎么能使其在320 * 480的视图旋转平稳? – Tariq 2011-05-24 10:53:50