放大时看到大面积的 UIScrollView 背景向下平移

发布于 2024-10-20 15:52:37 字数 3945 浏览 1 评论 0原文

问题

我遇到的问题是当我放大 UIScrollView(启用分页),然后向下平移(滚动?)时 – 我可以拖动到视图之外并看到大区域滚动视图的背景。放大得越远,UIScrollView 的背景就会暴露得越多。 (我已将滚动视图的背景设置为绿色,这样我就可以知道我在看什么)。

不知道为什么背景区域会这样暴露......?

我尝试过在不同位置设置滚动视图的框架大小、边界大小、内容大小等,但无济于事。

不幸的是,由于是新用户,我无法发布所发生情况的屏幕截图:(

更多详细信息:

我的应用程序的主视图包含一个全屏大小的 UIScrollView,其中填充了许多 UIView 子视图。这些子视图被添加到通过在 for 循环中创建 nib 名称,然后使用 loadNibNamed 方法检索它们来创建和添加 UIView,

这些 UIView 中的每一个都是在 IB 中设计的,并包含以下内容: 一个 UIImageView,其图像属性设置为.jpg 图像,下面是另一个较小的 UIScrollView,以及在我的 imageview 之上的透明 UIView,其中包含一堆自定义 UIButtons

在填充我的滚动视图后,我设置了它的一些属性,例如 contentSize、pagingEnabled、min 和 maxZoomScale,等等,然后将其委托设置为 self (MainViewController

的相关方法):

@implementation MainViewController_iPhone

@synthesize scrollView, loadedNibView;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {   
    [super viewDidLoad];
    [self loadNibs];
    [self initializeOrientation];
}

-(void) loadNibs {
    currentPage = 0;

    for (int i = 0; i < NUM_VIEWS; i++) {
        loadedNibView = [self getNibView:i];        //load the UIView from the associated nib file
        loadedNibView.tag = i;                      //used later to retrieve view for current page
        [scrollView addSubview:loadedNibView];
    }

    //Set the content size to sum of the widths of the all UIViews
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * NUM_VIEWS, self.view.frame.size.height);
    scrollView.pagingEnabled = YES;
    scrollView.bounces = YES;
    scrollView.clipsToBounds = YES;                 
    //scrollView.alwaysBounceVertical = YES;

    //zoom features
    scrollView.maximumZoomScale = 5.0;
    scrollView.minimumZoomScale = 1;                

    scrollView.delegate = self;                     
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)myScrollView {   
    return [myScrollView viewWithTag:(currentPage)];
}

- (void)scrollViewDidScroll:(UIScrollView *)myScrollView {
    CGFloat pageWidth = myScrollView.frame.size.width;
    currentPage = floor((myScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}

- (void)scrollViewDidZoom:(UIScrollView *)myScrollView {
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)myScrollView withView:(UIView *)view
{   
    //zooming & paging do not work well together; disable paging while we're zooming
    myScrollView.pagingEnabled = NO;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)myScrollView withView:(UIView *)view atScale:(float)scale  
{   
    if(scale == 1)  //if we're zoomed entirely back out
    {
    //re-enable paging & reset the content size
    myScrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * NUM_VIEWS, self.view.frame.size.height);  //without this line, can no longer page through scrollview after zooming
    }
} 


@end

并且它是 .h:

@interface MainViewController_iPhone : MainViewController <UIScrollViewDelegate> {  
    IBOutlet UIScrollView *scrollView;          //The main ScrollView that scrolls between pages of different images to search
    UIView *loadedNibView;                      //The UIView that we load from it's corresponding nib (i.e. IMG1_iPhone.xib)
    int currentPage;
}

//methods
-(void) loadNibs;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)myScrollView;
- (void)scrollViewWillBeginZooming:(UIScrollView *)myScrollView withView:(UIView *)view;
- (void)scrollViewDidEndZooming:(UIScrollView *)myScrollView withView:(UIView *)view atScale:(float)scale;
- (void)scrollViewDidZoom:(UIScrollView *)myScrollView;
- (void)scrollViewDidScroll:(UIScrollView *)myScrollView;

//properties
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIView *loadedNibView;

@end

任何想法/建议将不胜感激......谢谢!

Problem:

The problem I’m having is when I zoom into my UIScrollView (with paging enabled), and then I pan (scroll?) down – I can drag well past my view and see a large area of the scrollview’s background. The further I'm zoomed-in, the more of the UIScrollView's background is exposed.
(I have set the scrollview's background to green so I can tell what I’m looking at).

Not sure why the background area is exposed like that...?

I've played around with setting the scrollview's frame size, bounds size, content size etc. in various locations to no avail.

Unfortunately I cannot post screenshots of whats happening due to being a new user :(

Further details:

My app’s main view contains a full-screen sized UIScrollView populated with a number of UIView subviews. These subviews are added to the scrollview by creating the nib names in a for loop, and then retrieving them using the loadNibNamed method to create & add the UIView.

Each of these UIViews were designed in IB and contain the following: A UIImageView whose image attribute is set to a .jpg image, another smaller UIScrollView below that, and a transparent UIView over top of my imageview which contains a bunch of custom UIButtons.

After I populate my scrollView, I set some of it’s attributes such as contentSize, pagingEnabled, min & maxZoomScale, etc. & then set it’s delegate to self (MainViewController).

MainViewController's relevant methods:

@implementation MainViewController_iPhone

@synthesize scrollView, loadedNibView;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {   
    [super viewDidLoad];
    [self loadNibs];
    [self initializeOrientation];
}

-(void) loadNibs {
    currentPage = 0;

    for (int i = 0; i < NUM_VIEWS; i++) {
        loadedNibView = [self getNibView:i];        //load the UIView from the associated nib file
        loadedNibView.tag = i;                      //used later to retrieve view for current page
        [scrollView addSubview:loadedNibView];
    }

    //Set the content size to sum of the widths of the all UIViews
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * NUM_VIEWS, self.view.frame.size.height);
    scrollView.pagingEnabled = YES;
    scrollView.bounces = YES;
    scrollView.clipsToBounds = YES;                 
    //scrollView.alwaysBounceVertical = YES;

    //zoom features
    scrollView.maximumZoomScale = 5.0;
    scrollView.minimumZoomScale = 1;                

    scrollView.delegate = self;                     
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)myScrollView {   
    return [myScrollView viewWithTag:(currentPage)];
}

- (void)scrollViewDidScroll:(UIScrollView *)myScrollView {
    CGFloat pageWidth = myScrollView.frame.size.width;
    currentPage = floor((myScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}

- (void)scrollViewDidZoom:(UIScrollView *)myScrollView {
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)myScrollView withView:(UIView *)view
{   
    //zooming & paging do not work well together; disable paging while we're zooming
    myScrollView.pagingEnabled = NO;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)myScrollView withView:(UIView *)view atScale:(float)scale  
{   
    if(scale == 1)  //if we're zoomed entirely back out
    {
    //re-enable paging & reset the content size
    myScrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * NUM_VIEWS, self.view.frame.size.height);  //without this line, can no longer page through scrollview after zooming
    }
} 


@end

And it's .h:

@interface MainViewController_iPhone : MainViewController <UIScrollViewDelegate> {  
    IBOutlet UIScrollView *scrollView;          //The main ScrollView that scrolls between pages of different images to search
    UIView *loadedNibView;                      //The UIView that we load from it's corresponding nib (i.e. IMG1_iPhone.xib)
    int currentPage;
}

//methods
-(void) loadNibs;
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)myScrollView;
- (void)scrollViewWillBeginZooming:(UIScrollView *)myScrollView withView:(UIView *)view;
- (void)scrollViewDidEndZooming:(UIScrollView *)myScrollView withView:(UIView *)view atScale:(float)scale;
- (void)scrollViewDidZoom:(UIScrollView *)myScrollView;
- (void)scrollViewDidScroll:(UIScrollView *)myScrollView;

//properties
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIView *loadedNibView;

@end

Any ideas/advice would be greatly appreciated...Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江挽川 2024-10-27 15:52:37

好吧,我知道我的问题是什么了。

我意识到,当我放大第一张图像时,它给了我奇怪的行为,滚动视图背景随着我的缩放而增长,但如果我放大第二张图像 - 没有奇怪的行为。所以我开始想知道这两个图像之间有什么区别......并意识到它一定是标签。在 for 循环中我加载视图 &设置他们的标签我将第一个设置为零,并从那里递增。哎呀 - 有点忘了还有很多其他的观点&默认标签为零的对象!所以后来当它请求带有标签 0 的视图时 - 它返回了一些其他视图 & 。那是被调整大小的而不是我的图像。

使固定:

#define ZOOM_TAG 1000

...


for (int i = 0; i < NUM_VIEWS; i++) {
    loadedNibView = [self getNibView:i];
    //loadedNibView.tag = i
    loadedNibView.tag = i + ZOOM_TAG;  //used later to retrieve view for current page
    [scrollView addSubview:loadedNibView];
}

...

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

    //return [myScrollView viewWithTag:(currentPage)];
    return [myScrollView viewWithTag:(currentPage + ZOOM_TAG)];

}

Ok, I figured out what my problem was.

I realized that when I zoomed-in on the first image, it was giving me the weird behaviour with the scrollview background growing as I zoom, but if I zoomed-in on the second image - no weird behaviour. So I started wondering what is the difference between the two images... and came to realize it must be the tags. In the for loop where I load the views & set their tags I was setting the first one to zero, and incrementing from there. Oops - kinda forgot that there are a bunch of other views & objects that have a default tag of zero! So later on when it asked for the view with tag 0 - it was returning some other view & that was the one getting resized instead of my image.

Fix:

#define ZOOM_TAG 1000

...


for (int i = 0; i < NUM_VIEWS; i++) {
    loadedNibView = [self getNibView:i];
    //loadedNibView.tag = i
    loadedNibView.tag = i + ZOOM_TAG;  //used later to retrieve view for current page
    [scrollView addSubview:loadedNibView];
}

...

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

    //return [myScrollView viewWithTag:(currentPage)];
    return [myScrollView viewWithTag:(currentPage + ZOOM_TAG)];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文