缩放和旋转UIImageView

时间:2012-03-02 09:30:12

标签: objective-c uiimageview uiimage rotation zooming

我想缩放和旋转UIImageView。这是我的代码:

@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];
}

但是当我缩放时,UIImageView从View退出。谁能帮我?提前谢谢!

1 个答案:

答案 0 :(得分:1)

我有这样的问题,这两个功能将解决你的问题

- (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);
}