带有分页的Scrollview

时间:2016-01-07 11:29:41

标签: ios objective-c uiscrollview

我在故事板中放置了一个scrollview,我也启用了分页,我为scrollview做了一个插座,我以编程方式添加了4个uiviews。现在我需要在水平方向上分配4个uiview。请帮我做... 提前致谢

这是我的代码

- (void)scrollingView
{
    UIView *pageView1 = [[UIView alloc]init];    
    pageView1.frame = CGRectMake(0,0,self.scrollView.frame.size.width,self.scrollView.frame.size.height);    

    pageView1.backgroundColor = [UIColor blueColor];
    [self.scrollView addSubview:pageView1];

    UIView *pageView2 = [[UIView alloc]init];
    pageview2.frame = CGRectMake(0,0,self.scrollView.frame.size,width,self.scrollView.frame.size.height);

    pageView2.backgroundColor =[UIColor GrayColor];
    [self.scrollView addSubview:pageView2];
}

3 个答案:

答案 0 :(得分:1)

xPosition = 0;
    for (int i = 0; i<3; i++)
    {
        scrollView.clipsToBounds = YES;
        //Views
        UIView *views = [[UIView alloc]initWithFrame:CGRectMake(xPosition, 0, selfView.frame.size.width, scrollView.frame.size.height)];
        views.clipsToBounds = YES;

        [arrayOfViews addObject:views];
        xPosition += selfView.frame.size.width;

        [scrollView addSubview:views];
    }
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * arrayOfViews.count, scrollView.frame.size.height);


并实现scrollview委托方法

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWidth = self.scrollView.frame.size.width;
    float fractionalPage = self.scrollView.contentOffset.x / pageWidth;
    NSInteger page = lround(fractionalPage);
    self.pageCnt.currentPage = page;
}

答案 1 :(得分:0)

-(void)viewDidLayoutSubviews
{
    UIImage *image1=[UIImage imageNamed:@"1.jpeg"];
    UIImage *image2=[UIImage imageNamed:@"2.jpeg"];
    UIImage *image3=[UIImage imageNamed:@"7.jpeg"];
    UIImage *image4=[UIImage imageNamed:@"6.jpeg"];
  //  UIImage *image5=[[UIImage alloc]init];
    NSArray *imagesArray=[[NSArray alloc]initWithObjects:image1,image2,image3,image4,nil];

    for (int i=0; i<=3; i++) {
        UIImageView *imgView=[[UIImageView alloc]init];
        imgView.backgroundColor=[UIColor grayColor];
        imgView.contentMode=UIViewContentModeScaleToFill;
        imgView.alpha=0.5f;
        imgView.frame=CGRectMake(i*self.scrollViewSlider.bounds.size.width, 0, self.scrollViewSlider.bounds.size.width, self.scrollViewSlider.bounds.size.height);

        [imgView setImage:imagesArray[i]];

        [self.scrollViewSlider addSubview:imgView];
    }
    self.scrollViewSlider.pagingEnabled=TRUE;
    [self.scrollViewSlider setContentSize:CGSizeMake(imagesArray.count * self.scrollViewSlider.bounds.size.width, self.scrollViewSlider.bounds.size.height)];




}



- (void) scrollViewDidEndDecelerating: (UIScrollView *) __unused scrollView
{
    CGFloat pageWidth = self.scView.frame.size.width;
    self.pageControl.currentPage = (self.scView.contentOffset.x + pageWidth / 2) / pageWidth;
}

答案 2 :(得分:0)

试试这个你会得到它的工作

标题文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property (weak, nonatomic) IBOutlet UIScrollView *scView;


@end

实施:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}




-(void)viewDidLayoutSubviews
{

    NSArray *imagesArray=[NSArray arrayWithObjects:
                          [UIColor colorWithRed:246/255.0 green:155/255.0 blue:0/255.0 alpha:1],
                          [UIColor colorWithRed:129/255.0 green:195/255.0 blue:29/255.0 alpha:1],
                          [UIColor colorWithRed:62/255.0 green:173/255.0 blue:219/255.0 alpha:1],
                          [UIColor colorWithRed:229/255.0 green:66/255.0 blue:115/255.0 alpha:1],
                          [UIColor colorWithRed:148/255.0 green:141/255.0 blue:139/255.0 alpha:1],nil];

    for (int i=0; i<imagesArray.count; i++) {
        UIView *containerView=[[UIView alloc]init ];
        //containerView.backgroundColor=[UIColor redColor]
        containerView.backgroundColor=imagesArray[i % imagesArray.count];
        containerView.contentMode=UIViewContentModeScaleToFill;
        containerView.alpha=0.5f;
        containerView.frame=CGRectMake(i*self.scView.bounds.size.width, 0, self.scView.bounds.size.width, self.scView.bounds.size.height);

        //[imgView setImage:imagesArray[i]];

       [self.scView addSubview:containerView];
    }
    self.scView.pagingEnabled=TRUE;
    NSLog(@"%ld",imagesArray.count);
    NSLog(@"%ld",imagesArray.count);
    [self.scView setContentSize:CGSizeMake(imagesArray.count * self.scView.bounds.size.width, self.scView.bounds.size.height)];




}

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

- (void) scrollViewDidEndDecelerating: (UIScrollView *) __unused scrollView
{

    CGFloat pageWidth = self.scView.frame.size.width;
    self.pageControl.currentPage = (self.scView.contentOffset.x + pageWidth / 2) / pageWidth;

    NSLog(@"%f",pageWidth);
}