广告轮播

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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值