`

广告轮播

    博客分类:
  • ios
 
阅读更多
h
<UIAlertViewDelegate,UIScrollViewDelegate>{
    NSInteger currentIndex;
    int PAGENUM;
}

@property (strong, nonatomic) IBOutlet UIScrollView *bgScrolllview;
@property (strong, nonatomic) IBOutlet UIScrollView *imgScrollview;
@property (strong, nonatomic)  UIPageControl *page;
m
调用 [self _initImgScrollView ];

#pragma mark 初始化imgscrollview
-(void)_initImgScrollView{
    [_imgScrollview setFrame:CGRectMake(0, 0, ScreenW, 100)];
    [_imgScrollview setContentSize:CGSizeMake(ScreenW*4, 100)];
    [_imgScrollview setDelegate:self];
   
    _imgScrollview.pagingEnabled=YES;
    _imgScrollview.scrollEnabled=YES;
    _imgScrollview.showsHorizontalScrollIndicator=NO;
   
    int imgNum = 4;//可以动态获取
    for (int i=0; i<imgNum; i++) {
        UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(i*ScreenW, 0, ScreenW, 100)];
        [imgview setImage:[UIImage imageNamed:@"flashpic"]];
        [_imgScrollview addSubview:imgview];
    }
   
    PAGENUM =imgNum;
   
    //定义PageController 设定总页数,当前页,定义当控件被用户操作时,要触发的动作。
    _page = [[UIPageControl alloc]initWithFrame:CGRectMake(ScreenW/2-10, 90, 10, 5)];
    _page.numberOfPages = PAGENUM;
    _page.currentPage = 0;
    _page.currentPageIndicatorTintColor=[UIColor greenColor];
    _page.pageIndicatorTintColor=[UIColor whiteColor];
    [_page addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
   
    [_bgScrolllview insertSubview:_page atIndex:1];
   
    //使用NSTimer实现定时触发滚动控件滚动的动作。
    currentIndex = 0;
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
}

#pragma mark 滚图的动画效果
-(void)pageTurn:(UIPageControl *)aPageControl{
    int whichPage = aPageControl.currentPage;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [_imgScrollview setContentOffset:CGPointMake(ScreenW * whichPage, 0.0f) animated:YES];
    [UIView commitAnimations];
    _page.currentPage=whichPage;
}
#pragma page跟随scrollview滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1{
    CGPoint offset=scrollView1.contentOffset;
    CGRect bounds=scrollView1.frame;
    [_page setCurrentPage:offset.x/bounds.size.width];
    currentIndex=_page.currentPage;
}
#pragma mark 定时滚动
-(void)scrollTimer{
    currentIndex ++;
    if (currentIndex == PAGENUM) {
        currentIndex = 0;
    }
    _page.currentPage=currentIndex;
    [_imgScrollview scrollRectToVisible:CGRectMake(currentIndex * ScreenW, 0, ScreenW, 100) animated:YES];
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics