English 中文(简体)
UIScrollView - 防止作为 UIScrollView 子视图的 UIImage View 被拖离屏幕
原标题: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".

这是我的滚动视图初始化 :

- (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;}
最佳回答

使用这些代表方法:

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

然后读一下偏移符,如果它离开“安全区”,就返回。

也可能是您需要另一种来自 UIScrollView 的代表方法,

问题回答

试设置滚动视图上的弹跳 :

self.bounces = NO;

TRY this: ** **

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

** **

尝试将其 b盎司 属性设置为 no





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签