UIScrollView自动循环滚动

在类的申明文件(.h)里添加对Page控制器的申明:
@property (strong, nonatomic) IBOutlet UIPageControl *page;
@property (strong, nonatomic) IBOutlet UIScrollView *imageScrollView;
然后在实现文件(.m)里添加 对page对象的
@synthesize page;
@synthesize imageScrollView;
实现page对象的自动存取器。
改写viewDidLoad方法如下
- (void)viewDidLoad
{
    [super viewDidLoad];
    //这里定义了滚动视图的大小,是否支持翻页,是否显示水平滚动标示,委托对象是哪个
    imageScrollView.contentSize = CGSizeMake(PAGENUM * 320.0f, imageScrollView.frame.size.height);
    imageScrollView.pagingEnabled = YES;
    imageScrollView.showsHorizontalScrollIndicator = NO;
    imageScrollView.delegate = self;

    //这里为滚动视图添加了子视图,为了能添加后续操作,我这里定义的子视图是按键UIButton
    for (int i = 0; i < PAGENUM; i++) {
        NSString * fileName = [NSString stringWithFormat:@"%d.jpg",i+1];
        UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(i * 320.0f,  0.0f, 320.0f, 218.0f)];
        [imageButton setBackgroundImage:[UIImage imageNamed:fileName] forState:UIControlStateNormal];
        imageButton.tag = 900 + i;
        [imageScrollView addSubview:imageButton];
    }

    //定义PageController 设定总页数,当前页,定义当控件被用户操作时,要触发的动作。
   �0�2page.numberOfPages = PAGENUM;
    page.currentPage = 0;
    [page addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

    //使用NSTimer实现定时触发滚动控件滚动的动作。
    timeCount = 0;
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];

}
增加两个翻页动画和自动翻页的函数
//滚图的动画效果
-(void)pageTurn:(UIPageControl *)aPageControl{
    int whichPage = aPageControl.currentPage;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [imageScrollView setContentOffset:CGPointMake(320.0f * whichPage, 0.0f) animated:YES];
    [UIView commitAnimations];
}

//定时滚动
-(void)scrollTimer{
    timeCount ++;
    if (timeCount == PAGENUM) {
        timeCount = 0;
    }
	//chenyong 在这个地方加上self.pageController.currentpage = timeCount; 即可改变pageCon了,切记不要加到timeCount++的下边
    [imageScrollView scrollRectToVisible:CGRectMake(timeCount * 320.0, 65.0, 320.0, 218.0) animated:YES];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值