adi*_*dit 7 iphone objective-c uiscrollview ipad ios
所以我有一个UIImageView子视图UIScrollView,我设置contentSize为小于UIImageView宽度和高度,但它不允许我滚动..为什么这?这是一些代码:
UIImage * image = [UIImage imageWithData:data];
UIImageView * imgView = [[UIImageView alloc] initWithImage:image];
[imgView setUserInteractionEnabled:YES];
//[imgView setContentMode:UIViewContentModeScaleAspectFill];
[imgView setBackgroundColor:[UIColor clearColor]];
[imgView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[imgView setFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
CGRect imgFrame;
imgFrame.size.width = originalImageSize.width;
imgFrame.size.height = originalImageSize.height;
imgFrame.origin.x = imageOriginPoint.x;
imgFrame.origin.y = imageOriginPoint.y;
UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:imgFrame];
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:imgView];
[imgScrollView setBackgroundColor:[UIColor clearColor]];
[imgScrollView setFrame:imgFrame];
[imgScrollView setContentSize:CGSizeMake(200, 200)];
SCROLL VIEW CONTENT SIZE WIDTH IS 200.000000 AND HEIGHT CONTENT SIZE IS 200.000000
UIIMAGE VIEW WIDTH IS 2240.225830 AND HEIGHT IS 2240.225830
SCROLL VIEW FRAME WIDTH IS 768.000000 AND SCROLL VIEW FRAME HEIGHT IS 768.000061
Run Code Online (Sandbox Code Playgroud)
任何的想法?
Lin*_*ang 11
那是因为滚动视图边界的大小大于内容大小.内容大小需要是图像视图的实际大小,可见大小由帧/边界设置.我想你想在{200,200}大小的矩形中滚动图像?试试这个:
UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; // this is your visible rect
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:imgView];
[imgScrollView setBackgroundColor:[UIColor yellowColor]];
[imgScrollView setContentSize:imgFrame.size]; // this is your image view size
Run Code Online (Sandbox Code Playgroud)
我知道这是旧的,但我想指出,当你创建一个UIScrollViewin代码时,最重要的事情是:
scrollView.scrollEnabled = YES;
如果你没有这样做,你contentSize就无关紧要了.
小智 5
这样做的麻烦方法是始终将contentSize设置为大于视图大小。但是Swift中有一个属性,无论内容大小如何,scrollview始终可以“滚动”:
scrollView.alwaysBounceVertical = true
Run Code Online (Sandbox Code Playgroud)
还有一个用于水平滚动
| 归档时间: |
|
| 查看次数: |
19121 次 |
| 最近记录: |