is there anyway we can do rotation of image in a p

2019-03-22 07:13发布

问题:

is there anyway we can do rotation of image in a pendulum effect in iphone using UIView Animation or CoreAnimation

回答1:

Use CGAffineTransformRotate to rotate your UIImageView e.g.

yourImage.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI * (_angle/ 180.0));

Also, you'll need to set up the anchor point on your image, prior to rotating it, so that you can make it swing from the correct position. e.g.

yourImage.layer.anchorPoint = CGPointMake(0.5,0);  // Set anchor to top middle.

Obviously, you'll need to set up a timer to adjust move the image and control the angle. You could do something like the following in a timer. (untested)

_angle = 45;  // Set starting angle.
_direction = 1; // Set starting direction.
-(void) movePend
{
   if(_direction == 45){
      _direction = 1;
   } else if(_direction == 180) {
     _direction = 0;
   }
   _angle = (_direction) ? _angle-- : _angle++;  // Determine which way to rotate.
    yourImage.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI * (_angle/ 180.0));
}