gpt4 book ai didi

ios - UIScrollView内容不缩放

转载 作者:行者123 更新时间:2023-11-29 04:00:51 24 4
gpt4 key购买 nike

我在 UIScrollview 中有一个 UIImageView,当我尝试捏合和缩放时,图像会向下和向右跳跃(我认为坐标为 0,0 而不是居中),但保持相同的大小,并且在相同的位置,直到我停止捏,然后它就会回到原来的中心位置。

我让 NSLog 在缩放时打印出 ZoomScale,并且一直打印 0.5 直到我放手,然后它打印 1.0 一次。

我真的很困惑,所有关于这方面的教程看起来都很基础和简单,我不知道我哪里错了。

注意事项: ScrollView 和 ImageView 都在 Storyboard中,连接到 socket 。它们所在的 viewController 是一个实现 viewForZoomingInScrollView: 的 UIScrollView 委托(delegate)。 minimumScale 为 1.0,maximumScale 为 4.0,尽管这些数字似乎没有任何效果。

最佳答案

将此代码放入viewDidLoad

yourScroll.bouncesZoom = YES;
yourScroll.delegate = self;
yourScroll.clipsToBounds = YES;

UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];

[twoFingerTap setNumberOfTouchesRequired:2];

[yourImageView addGestureRecognizer:twoFingerTap];

float minimumScale = 1.0;//This is the minimum scale, set it to whatever you want. 1.0 = default

yourScroll.maximumZoomScale = 4.0;
yourScroll.minimumZoomScale = minimumScale;
yourScroll.zoomScale = minimumScale;
[yourScroll setContentMode:UIViewContentModeScaleAspectFit];
[yourScroll sizeToFit];
[yourScroll setContentSize:CGSizeMake(yourImageView.frame.size.width, yourImageView.frame.size.height)];

添加Scrollview和Gesture的委托(delegate)方法

#pragma mark UIScrollViewDelegate methods

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

return yourImageView;
}

#pragma mark TapDetectingImageViewDelegate methods

- (void)scrollViewDidZoom:(UIScrollView *)aScrollView {
CGFloat offsetX = (yourScroll.bounds.size.width > yourScroll.contentSize.width)?
(yourScroll.bounds.size.width - yourScroll.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = (yourScroll.bounds.size.height > yourScroll.contentSize.height)?
(yourScroll.bounds.size.height - yourScroll.contentSize.height) * 0.5 : 0.0;
yourImageView.center = CGPointMake(yourScroll.contentSize.width * 0.5 + offsetX,
yourScroll.contentSize.height * 0.5 + offsetY);
}


- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
// two-finger tap zooms out
float newScale = [previewScroll zoomScale] / ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[yourScroll zoomToRect:zoomRect animated:YES];
}

#pragma mark Utility methods

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {

CGRect zoomRect;

// the zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible, the size of the rect grows.
zoomRect.size.height = [previewScroll frame].size.height / scale;
zoomRect.size.width = [previewScroll frame].size.width / scale;

// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);

return zoomRect;
}

关于ios - UIScrollView内容不缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15861031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com