ios scrollView中增加动画效果,自动滚动UIScrollView,利用了NSTimer
转载自:http://blog.csdn.net/z343929897/article/details/7974753
按照赵总要求,在首页上边需要加广告条,本来以为挺复杂的,原来挺简单
@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;//这里为滚动视图添加了子视图,为了能添加后续操作,我这里定义的子视图是按键UIButtonfor (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];
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
