简体   繁体   中英

IOS Scrollview Marquee style

I have a set of 4 images that i want to show in a UIScrollView . This scroll view contains ad banners. So they have to keep scrolling vertically down automatically similar to marquee effect - user does not need to touch the screen to scroll.This process should keep repeating.

If the user touches the screen, the scrolling should stop & when user re-touches the screen the scrolling should restart from where it had left.

There is as simpler way, I guess. UIImageView can help you. Follow these Steps :

1) Add 4 UIImageView s Vertically Down.

2) Create 4 Different Arrays for these ImageViews.

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) Provide these Arrays to all these different ImageViews.

yourImageView1.animationImages = frames1;
yourImageView2.animationImages = frames2;
yourImageView3.animationImages = frames3;
yourImageView4.animationImages = frames4;

4) You can add some Effects also...

// all frames will execute in 1.75 seconds
yourImageView.animationDuration = 1.75;
// repeat the annimation forever
yourImageView.animationRepeatCount = 0;

5) Simply startAnimating the ImageViews.

[yourImageView1 startAnimating]; 
[yourImageView2 startAnimating]; 
[yourImageView3 startAnimating]; 
[yourImageView4 startAnimating]; 

6) You can always use -touchesEnded method to start and stop 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
    }
}

I think this will give you the effect what you want.

GoodLuck !!!

This is how I have done it finally :-) .
Call the below method from viewDidLoad

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollTheScrollView) userInfo:nil repeats:YES];

Now implement the method

-(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)];}

I made an API for this, if you're interested.

https://github.com/dokun1/DOMarqueeLabel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM