0
在我的UIScrollview我有一個UIImageView黑色背景。裏面它有一個與裏面的UIImage另一個的UIImageView和我使用此代碼,以使變焦:縮放UIImageView和移動縮放
fullImageView = [[UIView alloc] initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)];
fullImageView.backgroundColor = [UIColor blackColor];
[fullImageView addSubview:copyImageToShowImageView];
zoomFullImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)];
[zoomFullImageScrollView addSubview:fullImageView];
[self.view addSubview:zoomFullImageScrollView];
UIPinchGestureRecognizer *twoFingerTapRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapImage:)];
[copyImageToShowImageView addGestureRecognizer:twoFingerTapRecognizer];
//Then..
- (void)doubleTapImage:(UIPinchGestureRecognizer *)gesture{
if (gesture.state == UIGestureRecognizerStateEnded
|| gesture.state == UIGestureRecognizerStateChanged) {
CGFloat currentScale = copyImageToShowImageView.frame.size.width/copyImageToShowImageView.bounds.size.width;
CGFloat newScale = currentScale * gesture.scale;
NSLog(@"gesture.scale = %f", currentScale * gesture.scale);
if (newScale < 1) {
newScale = 1;
}
if (newScale > 100) {
newScale = 100;
}
CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
copyImageToShowImageView.transform = transform;
gesture.scale = 1;
copyImageToShowImageView.transform = CGAffineTransformTranslate(copyImageToShowImageView.transform, newScale, newScale);
zoomFullImageScrollView.contentSize=CGSizeMake(copyImageToShowImageView.frame.size.width, copyImageToShowImageView.frame.size.height);
}
}
//and the methods:
- (void)viewDidLoad {
NSLog(@"super");
[super viewDidLoad];
zoomFullImageScrollView.minimumZoomScale=0.5;
zoomFullImageScrollView.maximumZoomScale=6.0;
zoomFullImageScrollView.delegate=self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return copyImageToShowImageView;
}
此代碼的工作,但有一個問題,因爲滾動視圖內容大小從UIImageView的大小不同。也許他們平等但不一致。然後我想,當用戶進行縮放,圖像放大那裏,並不總是從中心,但這並不重要..
我希望我解釋我自己。
我該怎麼辦?
我更新我的答案,因爲我有另一個問題,使用滾動視圖 – lubilis
檢查[this](http://www.timoliver.com.au/2012/01/14/zooming-to-a-point-in-uiscrollview/ )解決方案。此類別可以縮放到特定點。我認爲你不需要應用那些正在應用的轉換。 –
好吧,我解決了與UIScrollView及其委託方法,謝謝! – lubilis