简体   繁体   English

UIScrollView - 防止UIImageView作为UIScrollView的子视图被拖出屏幕

[英]UIScrollView - Prevent UIImageView as subview of UIScrollView from being dragged off screen

I have a UIImageView element as a subview of my main UIScrollView element. 我有一个UIImageView元素作为我的主要UIScrollView元素的子视图。 I want the Image to fill out the whole screen at all times, but I can drag the edges a bit too far, so that the yellow "background" becomes visible. 我希望Image始终填满整个屏幕,但我可以将边缘拖得太远,以便黄色“背景”变得可见。 If I lift my finger, the Image "bounces" back and fills out the screen correctly. 如果我抬起手指,图像会“弹回”并正确填满屏幕。

I want to prevent this dragging of the image off screen. 我想阻止这种图像拖离屏幕。 I do however want the image to bounce back once I drag it out of the "safety area". 但是,当我将图像拖出“安全区域”时,我希望图像能够反弹。

This is my ScrollView initialization: 这是我的ScrollView初始化:

- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:CGRectMake(0, 0, frame.size.height, frame.size.width)];
if (self) {
    [self initImageValues];

    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.bouncesZoom = YES;
    self.decelerationRate = UIScrollViewDecelerationRateFast;
    self.delegate = self;
    self.backgroundColor = [UIColor yellowColor];
    self.minimumZoomScale = 1.0;
    self.maximumZoomScale = 2.0;
    [self setCanCancelContentTouches:YES];
    self.clipsToBounds = YES;

    // Load Image
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    [self addSubview:imageView];

    [self setContentSize: CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];

    // Set bigger "bounce" zone (safety area)
    self.contentInset=UIEdgeInsetsMake(-SAFETY_ZONE,-SAFETY_ZONE,-SAFETY_ZONE,-SAFETY_ZONE);

    self.scrollEnabled = YES;

}
return self;}

Use these delegate methods: 使用这些委托方法:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

Then read the offset and return if it's out of the "safety area". 然后读取偏移并返回它是否超出“安全区域”。

Could be that you need another delegate method from the UIScrollView though, but a workaround like this should fix your problem :) 可能是你需要来自UIScrollView的另一个委托方法,但这样的解决方法应该可以解决你的问题:)

尝试将其bounces属性设置为NO

尝试在scrollview上设置反弹:

self.bounces = NO;

TRY this: ** 尝试这个: **

self.scrollView.maximumZoomScale = 1.0;
self.scrollView.minimumZoomScale = 1.0;

** **

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM