UIScrollView - 图片轮播器

2017-08-28  本文已影响74人  js_huh

效果图

UIScrollView - 图片轮播.gif
需求:
1.图片横向手动滚动
2.图片横向定时滚动
3.TextVie的滚动,不影响UIScrollView
思路:
  1. 图片横向手动滚动
 UIImageView *imgViewText = [UIImageView alloc]initWithImage :XXX];
imgViewText.frame = CGRectMake(X,Y,W,H);
[self.scrollView addSubView:imgViewText];
CGFloat imgMaxX = CGRectGetMaxX(imgViewText.frame);
self.scrollView.contentSize = CGSizeMake(imgMaxX,0);

2.图片横向定时滚动

self.timerImgScroll = [NSTimer scheduledTimerWithTimeInterval: 2.0 
target:self 
selector: @selector(autoImgScoll) 
userInfo:nil 
repeats:YES];

每隔2秒,
调用self的autoImgScoll方法一次,
(self调用self的autoImgScoll方法一次)
不发送消息,
重复调用
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;//分页指示器
@property (assign, nonatomic) int pageNo; //分页码
@property(nonatomic) int  pageSum;// 总页数

-(void)autoImgScoll{
    self.pageNo ++; 
    if(self.pageNo >= self.pageSum){
        self.pageNo = 0;
    }
    CGFloat  offsetScroll = (CGFloat)self.pageNo * self.scrollVisibleW;
    self.scrollView.contentOffset = CGPointMake(offsetScroll, 0);
}

3.解决Bug


UIScrollView - 图片轮播器(完整代码)

上一篇 下一篇

猜你喜欢

热点阅读