UIView smooth rotation with touch

I am wondering how to smooth out UITouchin my code. I can detect UITouchon mine UIView, but when I try to rotate the view using CGAffineTransform, it does not rotate smoothly. I have to press or long touch the iPhone with my finger for this kind of rotation. How can I perform smooth rotations, for example, the Visualizer app for Roambi. Thank you for your help.

+3
source share
3 answers

transform is an animated property of UIView, so you can use Core Animation to make it rotate smoothly:

CGAffineTransform newTransform = //...construct your desired transform here...
[UIView animateWithDuration:0.2
                 animations:^{view.transform = newTransform;}];
+3
source

Hi everyone I found the following solution to my question and its work for me in touchhesMoved .....

Here is the code ....

UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self.superview];
CGPoint pastLocation = [touch previousLocationInView:self.superview];
currentLocation.x = currentLocation.x - self.center.x;
currentLocation.y = self.center.y - currentLocation.y;
pastLocation.x = pastLocation.x - self.center.x;
pastLocation.y = self.center.y - currentLocation.y;
CGFloat angle = atan2(pastLocation.y, pastLocation.x) - atan2(currentLocation.y, currentLocation.x); 
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);

// Apply the affine transform

[[self.superview viewWithTag:ROTATE_VIEW_TAG] setTransform:transform] ;
+1

, .

 pastLocation.y = self.center.y - currentLocation.y;

 pastLocation.y = self.center.y - pastLocation.y;

, Cam, :

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    var touch: UITouch = touches.first as! UITouch

    var currentTouch = touch.locationInView(self.view)
    var previousTouch = touch.previousLocationInView(self.view)

    currentTouch.x = currentTouch.x - self.view.center.x
    currentTouch.y = self.view.center.y - currentTouch.y

    previousTouch.x = previousTouch.x - self.view.center.x
    previousTouch.y = self.view.center.y - previousTouch.y

    var angle = atan2(previousTouch.y, previousTouch.x) - atan2(currentTouch.y, currentTouch.x)

    UIView.animateWithDuration(0.1, animations: { () -> Void in
        bigCircleView?.transform = CGAffineTransformRotate(bigCircleView!.transform, angle)
    })

}
+1
source

All Articles