我想在屏幕上随机旋转一个按钮。没有定义特定路径,它可以在视图上随机移动。
我不想使用 CAKeyframeAnimation。它应该是干净和简单的。
谁能指导我?
我想在屏幕上随机旋转一个按钮。没有定义特定路径,它可以在视图上随机移动。
我不想使用 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];
另请查看此链接是否对您有用:Moving UIButton
使用随机时间间隔为按钮的transform
属性设置动画CGAffineTransformMakeRotation()
并将其与 a 组合。NSTimer
#define RADIANS(degrees) ((degrees * M_PI) / 180.0)
CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,
RADIANS(30.0));
myButton.transform = rotateTransform;
使用 NSTimer 来承载这个过程,并使用 arc4random 随机生成以弧度为单位的角度。