【问题标题】:Image sizes not working with Page Control & ScrollView?图像尺寸不适用于 Page Control 和 ScrollView?
【发布时间】:2013-11-03 17:25:12
【问题描述】:

我正在尝试使用图像数组来实现页面控制和滚动视图,但由于某种原因,我的图像在滑动到下一页时会四处移动。看起来图像对于滚动视图来说太大了,但缩小它们似乎不起作用。有没有办法让 ScrollView 不会垂直“滚动”?请参阅下面的代码。

SSViewController.h

#import <UIKit/UIKit.h>

@interface SSViewController : UIViewController <UIScrollViewDelegate>

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;


@property (nonatomic, strong) NSArray *imageArray;

SSViewController.m

 -(void)viewDidAppear:(BOOL)animated
{

   imageArray = [[NSArray alloc] initWithObjects:@"first.png", @"second.png", nil];

for (int i = 0; i < [imageArray count]; i++) {
    //We'll create an imageView object in every 'page' of our scrollView.
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
    size:imageView.clipsToBounds = YES;
    imageView.contentMode = UIViewContentModeScaleAspectFill;

    [self.scrollView addSubview:imageView];
}
//Set the content size of our scrollview according to the total width of our imageView objects.
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imageArray count], scrollView.frame.size.height);
}


    - (void)scrollViewDidScroll:(UIScrollView *)sender
    {
        // Update the page when more than 50% of the previous/next page is visible
        CGFloat pageWidth = self.scrollView.frame.size.width;
        int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        self.pageControl.currentPage = page;
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);

    }

【问题讨论】:

    标签: objective-c uiviewcontroller uiscrollview uipagecontrol


    【解决方案1】:

    我认为将所有分配代码(尤其是所有代码定义框架)转移到viewWillAppear 方法是个好主意,因为在viewDidLoad 中,某些框架还没有设置。

    【讨论】:

    • 已将代码移至 viewWillAppear(参见上面的编辑代码),但我仍然遇到同样的问题。
    • 当你将图像设置为你的 imageView 后添加这两行来缩放图像到 imageView 帧大小:imageView.clipsToBounds = YES;imageView.contentMode = UIViewContentModeScaleAspectFill;
    • 啊修好了。将此行添加到我的 scrollViewDidScroll 的末尾: self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多