[英]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.