Question

I am developing an app, Which has feature that dragging and rotating the image view.

I have implemented to drag and rotate successfully by using below code

In touchesBegan Method

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];

    touchStart = [[touches anyObject] locationInView:imageView];
    isResizingLR = (containerVw.bounds.size.width - touchStart.x < kResizeThumbSize && containerVw.bounds.size.height - touchStart.y < kResizeThumbSize);

}

In touchesMoved Method

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

     CGPoint touchPoint = [[touches anyObject] locationInView:containerVw];
     CGPoint previous=[[touches anyObject]previousLocationInView:containerVw];
     UITouch *touch = [[event allTouches] anyObject];

     if (isResizingLR) {

        CGFloat currA = [self pointToAngleFromCenter:[touch locationInView:containerVw.superview]] ;
        CGFloat prevA = [self pointToAngleFromCenter:[touch previousLocationInView:containerVw.superview]] ;
        // the current value of the rotation angle
        CGFloat tranA = atan2(containerVw.transform.b, containerVw.transform.a) ;
        // the angle difference between last touch and the current touch
        CGFloat diffA = currA - prevA ;
        // the new angle resulting from applying the difference
        CGFloat angle1 = tranA - diffA ;

        CGAffineTransform t = CGAffineTransformMakeRotation(angle1) ;
        containerVw.transform =t;
       }

      if (!isResizingLR) {
         containerVw.center = CGPointMake(containerVw.center.x + touchPoint.x - touchStart.x,           containerVw.center.y + touchPoint.y - touchStart.y);

       }
}

My Problem

Above code is working fine until rotating the image view after dragging. In case if i drag the image view after rotating it. image view has gone away from screen.

In touchesEnd method i tried to print the image view frame. for example my image view frame at the beginning is (100,100,150,149) after rotating it the image view 160º frame has changed to (103,31,182,183) up to now its working fine.

After rotating it if try to move an image view, the image view has gone away from screen now the frame has changed to (615,-212,182,183)

Please guide me how do i resolve it.

Was it helpful?

Solution

Have a look at this project. This may help you. But here, the movement is handled by gestures and not by touch. It may solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top