如何通过编程方式创建水平图像的滚动视图?

时间:2015-12-07 10:40:03

标签: ios objective-c image uiscrollview

您好我是iOS新手如何以编程方式创建滚动视图

像这样,我试过,我得到水平图像的滚动视图,但图像放置如下图片

我做得对吗?

请帮帮我。 感谢

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
       // CGRectMake(0, 0, 600, 200);
    _scrollView.tag = 1;
    _scrollView.autoresizingMask=UIViewAutoresizingNone;
    NSLog(@"--->%@",_scrollView);

    [self.view addSubview:_scrollView];
    [self setupScrollView:_scrollView];
    UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 154, 480, 36)];
     NSLog(@"--->%@",pgCtr);
    [pgCtr setTag:12];
    pgCtr.numberOfPages=10;
    pgCtr.autoresizingMask=UIViewAutoresizingNone;
    [self.view addSubview:pgCtr];

    // Do any additional setup after loading the view, typically from a nib.
}
- (void)setupScrollView:(UIScrollView*)scrMain {
    // we have 10 images here.
    // we will add all images into a scrollView & set the appropriate size.

    for (int i=1; i<=3; i++) {
        // create image
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"abc%d.png",i]];

        // create imageView
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(((i-1)*scrMain.frame.size.height+5), 0, 320, 195)];

        // set scale to fill
        _imageView.contentMode=UIViewContentModeScaleToFill;
        // set image
        [_imageView setImage:image];
        // apply tag to access in future
        _imageView.tag=i+1;
          NSLog(@"--->%@",_imageView);
        // add to scrollView
        [scrMain addSubview:_imageView];
    }
    // set the content size to 10 image width
    [scrMain setContentSize:CGSizeMake(320*3, scrMain.frame.size.height)];
    NSLog(@"--->%@",scrMain);
    // enable timer after each 2 seconds for scrolling.
    [NSTimer scheduledTimerWithTimeInterval:7 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];


}
- (void)scrollingTimer {
    // access the scroll view with the tag
    UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
    NSLog(@"--->%@",scrMain);

    // same way, access pagecontroll access
    UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
    NSLog(@"--->%@",pgCtr);

    // get the current offset ( which page is being displayed )
    CGFloat contentOffset = 194;
    NSLog(@"------------------->%f",contentOffset);
    // calculate next page to display
    int nextPage = (int)(contentOffset/scrMain.frame.size.height) + 1 ;
    NSLog(@"%d",nextPage);
    // if page is not 10, display it
    if( nextPage!=10 )  {


        [scrMain scrollRectToVisible:CGRectMake(0, nextPage*scrMain.frame.size.height, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
         NSLog(@"--->%@",scrMain);
        pgCtr.currentPage=nextPage;
        // else start sliding form 1 :)


    } else {

        [scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
         NSLog(@"-------------------------------------------->%@",scrMain);
        pgCtr.currentPage=0;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3 个答案:

答案 0 :(得分:1)

我想,你错了:

// create imageView
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(((i-1)*scrMain.frame.size.height+5), 0, 320, 195)];

应该是:

// create imageView // change height to width
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(((i-1)*scrMain.frame.size.width+5), 0, 320, 195)];

答案 1 :(得分:0)

您可以向Scrollview添加自定义视图,在自定义视图中添加UIImageview作为子视图,左右填充一些?

答案 2 :(得分:0)

请实现此代码。希望这可以使用并使用这些宏。

#define SCREEN_HEIGHT  [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width

 - (void)setupScrollView:(UIScrollView*)scrMain
   {
       for (int i=0; i<3; i++)
       {
           UIImage *image = [UIImage imageNamed:[NSString   stringWithFormat:@"abc%d.png",i]];
           UIImageView *_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, 195)];
          _imageView.contentMode=UIViewContentModeScaleToFill;
          [_imageView setImage:image];
         _imageView.tag=i;
          [scrMain addSubview:_imageView];
     }

    [scrMain setContentSize:CGSizeMake(SCREEN_WIDTH*3, scrMain.frame.size.height)];
    [NSTimer scheduledTimerWithTimeInterval:7 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];
  }
相关问题