objective-ciosiphoneiphone-sdk-4.1

How to Rotate UIButton by tap and hold (hold - rotate by finger)


I'm noob in ios development. I need some help. I have custom UIButton with picture "arrow", so I need to rotate this button by pressing and moving finger in +360 gr. and -360 gr., like compass arrow.


Solution

  • Here is code that makes rotation.

    -(void)LongPress:(UILongPressGestureRecognizer *)gesture {
    
        CGPoint p = [gesture locationInView:self.view];
    
        CGPoint zero;
        zero.x = self.view.bounds.size.width / 2.0;
        zero.y = self.view.bounds.size.height / 2.0;
    
        CGPoint newPoint;
    
        newPoint.x = p.x - zero.x;
        newPoint.y = zero.y - p.y;
        CGFloat angle;
        angle = atan2(newPoint.x, newPoint.y);
        self.myButton.transform = CGAffineTransformRotate(CGAffineTransformIdentity, angle);
    
    }