当前位置: 首页 > 工具软件 > Page Scroller > 使用案例 >

ScrollView and pageControl

顾永福
2023-12-01

.h文件:

@property(strong,nonatomic) UIPageControl *pageControl;

@property(strong,nonatomic) UIScrollView *scrollView;

@property(strong,nonatomic) UIView *pView;




.m文件:

- (void)viewDidLoad

{

    [super viewDidLoad];

    self.pView = [[UIView alloc] init];

    self.pView.frame = CGRectMake(0, 0, 320, 504);

    

    [self.view addSubview:self.pView];

    

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

    self.scrollView.backgroundColor = [UIColor orangeColor];

    //设置内容大小

    self.scrollView.contentSize = CGSizeMake(320*2,50);

    //是否支持滑动最顶端

    self.scrollView.scrollsToTop = NO;

    //是否分页

    self.scrollView.pagingEnabled = YES;

    //是否反弹

    self.scrollView.bounces = NO;

    //是否滚动

    self.scrollView.scrollEnabled = YES;

    self.scrollView.showsHorizontalScrollIndicator = NO;

    self.scrollView.showsVerticalScrollIndicator = YES;

    //设置indicator风格

    self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

    //设置内容的边缘和Indicator边缘

    self.scrollView.contentInset = UIEdgeInsetsMake(0, 50, 50, 0);

    self.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 50, 0, 0);

    //提示用户,Indicators flash

    [self.scrollView flashScrollIndicators];

    //是否同时运动,lock

    self.scrollView.directionalLockEnabled = YES;


    self.scrollView.delegate = self;

    [self.pView addSubview:self.scrollView];

    

    //添加图片

    NSArray *imageArray = [NSArray arrayWithObjects:@"d.jpeg",@"psb.jpeg", nil];

    for (NSInteger i = 0; i < imageArray.count; i++) {

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320*i, 0, 320, 100)];

        imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];

        [self.scrollView addSubview:imageView];

    }

   

    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(110, 100, 120, 110)];

    NSInteger pageCount = [imageArray count];

    _pageControl.backgroundColor = [UIColor redColor];

    _pageControl.currentPage = 0;

    _pageControl.numberOfPages = pageCount;

    [self.pView addSubview:_pageControl];


}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    int page = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;

    [self.pageControl setCurrentPage:page];

}





 类似资料:

相关阅读

相关文章

相关问答