user1053839
user1053839

Reputation: 390

Zooming and Rotation an UIImageView

I would like to zoom and rotate an UIImageView. Here is my code:

@synthesize immagine, velocita, locationManager, direzione;

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
    double degrees = newHeading.magneticHeading;
    double radians = degrees * M_PI / 180;


    [UIView animateWithDuration:0.05 animations:^{
        self.immagine.transform = CGAffineTransformMakeRotation(-radians);
    }];
}

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return immagine;
}

- (void)viewDidLoad
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;

    [scrollView setDelegate:self];
    [scrollView setContentSize:CGSizeMake(320, 460)];

    immagine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mappa1"]];
    immagine.frame = CGRectMake(0, 0, 320, 460);
    immagine.contentMode = UIViewContentModeScaleAspectFit;

    [scrollView addSubview:immagine];

    locationManager.headingFilter = kCLHeadingFilterNone; 
    [locationManager startUpdatingHeading];

    [super viewDidLoad];
}

But when I'm zooming, the UIImageView exit from the View. Can anyone help me? Thanks in advance!

Upvotes: 2

Views: 834

Answers (1)

khaled
khaled

Reputation: 865

I have problem like this and these two function will solve your problem

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{     
    CGRect originalFrame=recognizer.view.frame;
    CGRect newFrame=CGRectMake(originalFrame.origin.x, originalFrame.origin.y,           originalFrame.size.width*recognizer.scale, originalFrame.size.height*recognizer.scale);
    if (newFrame.size.width>70&&newFrame.size.width<150) 
    {
        recognizer.view.frame=newFrame;
    }  
}

- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer 
{
    [self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)recognizer view]];

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

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

    CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)recognizer   view].transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

    [[(UIRotationGestureRecognizer*)recognizer view] setTransform:newTransform];

    lastRotation = [(UIRotationGestureRecognizer*)recognizer rotation];

    // recognizer.view.transform=CGAffineTransformMakeRotation(([recognizer rotation] * 180) / M_PI);
}

Upvotes: 1

Related Questions