Rotate UIButton randomly on screen
I want to rotate a button randomly on a screen. No specific path defined it can move randomly on a view.
I dont want to 开发者_Go百科use CAKeyframeAnimation. It should be clean and simple.
Can anyone guide me ?
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];
Also see if this link is useful to you : Moving UIButton
Animate the button's transform
property using CGAffineTransformMakeRotation()
and combine it with an NSTimer
with a random time interval.
#define RADIANS(degrees) ((degrees * M_PI) / 180.0)
CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,
RADIANS(30.0));
myButton.transform = rotateTransform;
Use NSTimer to carry this process and use arc4random to randomly generate the angle in radians.
精彩评论