在屏幕上随机旋转UIButton



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

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

有人能给我指路吗?

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];

另请查看此链接是否对您有用:移动UIButton

使用CGAffineTransformMakeRotation()为按钮的transform属性制作动画,并将其与具有随机时间间隔的NSTimer组合。

#define RADIANS(degrees) ((degrees * M_PI) / 180.0)
CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,
     RADIANS(30.0));
myButton.transform = rotateTransform;

使用NSTimer执行此过程,并使用arc4random随机生成以弧度为单位的角度。

最新更新