[英]IOS Scrollview Marquee style
我有一组要在UIScrollView
显示的4张图像。 此滚动视图包含广告横幅。 因此,他们必须像字幕效果一样自动保持垂直向下滚动-用户无需触摸屏幕即可滚动。此过程应不断重复。
如果用户触摸屏幕,则滚动应停止;当用户重新触摸屏幕时,滚动应从其离开的位置重新开始。
我猜有一种更简单的方法。 UIImageView可以为您提供帮助。 跟着这些步骤 :
1)垂直向下添加4个UIImageView
。
2)为这些ImageView创建4个不同的数组。
NSArray *frames1 = [NSArray arrayWithObjects:[UIImage imageWithName:@"a.png"],[UIImage imageWithName:@"b.png"],[UIImage imageWithName:@"c.png"],[UIImage imageWithName:@"d.png"],nil];
NSArray *frames2 = [NSArray arrayWithObjects:[UIImage imageWithName:@"b.png"],[UIImage imageWithName:@"c.png"],[UIImage imageWithName:@"d.png"],[UIImage imageWithName:@"a.png"],nil];
NSArray *frames3 = [NSArray arrayWithObjects:[UIImage imageWithName:@"c.png"],[UIImage imageWithName:@"d.png"],[UIImage imageWithName:@"a.png"],[UIImage imageWithName:@"b.png"],nil];
NSArray *frames4 = [NSArray arrayWithObjects:[UIImage imageWithName:@"d.png"],[UIImage imageWithName:@"a.png"],[UIImage imageWithName:@"b.png"],[UIImage imageWithName:@"c.png"],nil];
3)将这些数组提供给所有这些不同的ImageView。
yourImageView1.animationImages = frames1;
yourImageView2.animationImages = frames2;
yourImageView3.animationImages = frames3;
yourImageView4.animationImages = frames4;
4)您还可以添加一些效果...
// all frames will execute in 1.75 seconds
yourImageView.animationDuration = 1.75;
// repeat the annimation forever
yourImageView.animationRepeatCount = 0;
5)只需startAnimating
对ImageViews进行startAnimating
处理。
[yourImageView1 startAnimating];
[yourImageView2 startAnimating];
[yourImageView3 startAnimating];
[yourImageView4 startAnimating];
6)您始终可以使用-touchesEnded
方法来启动和停止Animation。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([[touches anyObject] view] == yourImageView1) {
//Here you can write the code to stop and start Animation of UIImageView
}
}
我认为这将为您带来想要的效果。
祝好运 !!!
这就是我最终完成的方法:-)。
从viewDidLoad调用以下方法
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollTheScrollView) userInfo:nil repeats:YES];
现在实现方法
-(void)scrollTheScrollView{
static int i=0;
i++;
if(self.scrollView.contentOffset.y == 700.0)//This 700.0 will be different for you
i=0;
NSLog(@"Content offset in scrolling method is %@",self.scrollView);
[self.scrollView setContentOffset:CGPointMake(0, i*5)];}
如果您有兴趣,我为此做了一个API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.