简体   繁体   中英

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. 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. 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:

- (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 :)

尝试将其bounces属性设置为NO

尝试在scrollview上设置反弹:

self.bounces = NO;

TRY this: **

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

**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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