Manohar Manohar - 2 months ago 17
iOS Question

move and rotate an image with fingers on a view ios xcode

Moving and rotating an image with finger's






- (void)viewDidLoad
{
self.imageview.image = [UIImage imageNamed:@"Image.png"];
[self.view addSubview: self.imageview];
[super viewDidLoad];
}


This code is to move image

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
self.imageview.center = touchLocation;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.PlayVideo];

if ([touch view] == self.view)
{

self.imageview.center = touchLocation;


}
}


This code is to rotate image (T dropped a rotation gesture on to the image and draghed on to .h file)

- (IBAction)regRotation:(UIGestureRecognizer *)sender
{
NSLog(@"rotation");
CGFloat netRotation =0.0;
CGFloat rotation =[(UIRotationGestureRecognizer *)sender rotation];
CGAffineTransform transform=CGAffineTransformMakeRotation(rotation+netRotation);
sender.view.transform=transform;
if(sender.state==UIGestureRecognizerStateEnded)
{
netRotation+=rotation;
}
}





The image is moving and not perfectly rotating

Answer
    CGFloat firstX;
    CGFloat firstY;
    CGFloat lastRotation;

    - (void)viewDidLoad
        {
lastRotation = 0.0;
            self.imageview.image = [UIImage imageNamed:@"Image.png"];
            [self.view addSubview: self.imageview];
            [super viewDidLoad];

    UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panRecognizer setMinimumNumberOfTouches:1];
        [panRecognizer setMaximumNumberOfTouches:1];
        [panRecognizer setDelegate:self];
        [self.imageview addGestureRecognizer:panRecognizer];

 UIRotationGestureRecognizer* rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
    [rotationRecognizer setDelegate:self];
    [self.view addGestureRecognizer:rotationRecognizer];

        }

    -(void)move:(id)sender {
        [self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
        CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

        if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
            firstX = [[sender view] center].x;
            firstY = [[sender view] center].y;
        }
        translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

        [[sender view] setCenter:translatedPoint];

        if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

            CGFloat finalX = translatedPoint.x + (0*[(UIPanGestureRecognizer*)sender velocityInView:self.imageview].x);
            CGFloat finalY = translatedPoint.y + (0*[(UIPanGestureRecognizer*)sender velocityInView:self.imageview].y);

            [[sender view] setCenter:CGPointMake(finalX, finalY)];
        }
    }
-(void)rotate:(id)sender {

    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        lastRotation = 0.0;
        return;
    }

    CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

    CGAffineTransform currentTransform = self.imageview.transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

    [self.imageview setTransform:newTransform];
    lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}

Its working for me..