您的位置:首页 > 移动开发 > IOS开发

iOS 之滚动视图的定时滚动

2013-10-09 11:46 302 查看
在类的申明文件(.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;
    }
    [imageScrollView scrollRectToVisible:CGRectMake(timeCount * 320.0, 65.0, 320.0, 218.0) animated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: