Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create marquee effect on imageview in Objc

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 514
    Comment on it

    To create marquee effect first set scroll view either via storyboard or via code
    Here in this example I am setting up scroll view via code.

    declare these variables

    •     UIScrollView *scrollView;
    •     NSTimer *timer;
    •     CGFloat contentOffset;

     

    1. Set up scroll view.

    -(void)configureScrollView //call this function when you need to set scrollView
    {
        float width = self.view.bounds.size.width;
        scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, 162)];
        scrollView.delegate = self;
        scrollView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:scrollView];
        scrollView.contentSize = CGSizeMake(width*self.imageArray.count,0);
        scrollView.pagingEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = YES;
        [self setupScrollView:scrollView];
    }

    2.

    - (void)setupScrollView:(UIScrollView*)scrMain {
       [self addImageArrayToScrollView:scrMain image:self.imageArray ];//send image array to this function.
        // enable timer after each 2 seconds for scrolling.
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollingTimer) userInfo:nil repeats:YES];
    }

     

    3. This function will set the image array  to index according to array count.

    -(void)addImageArrayToScrollView:(UIScrollView*)scrlView image:(NSArray*)imgarr {
        for(int index=0; index < [self.imageArray count]; index++)
        {
            NSDictionary *dict=[self.imageArray objectAtIndex:index];
            NSString *image=[dict valueForKey:@"image_slide"];
            UIImageView *bigImage=[[UIImageView alloc]init];
            bigImage.frame = CGRectMake(scrlView.bounds.size.width*index, 0, self.view.bounds.size.width, 162);
            [bigImage setImageWithURL:[NSURL URLWithString:[image stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]];
            [self.objectArray insertObject:bigImage atIndex:index];
            [scrlView addSubview:bigImage];
            [scrlView addSubview:[self.objectArray objectAtIndex:index]];
        }
    }

     

    4. To change the image after each 2 seconds

    - (void)scrollingTimer {
        // calculate next page to display
        int nextPage = (int)(contentOffset/scrollView.frame.size.width) + 1 ;
        // if page is not equals to the array count, display it
        if( nextPage <= self.imageArray.count )  {
            [scrollView setContentOffset:CGPointMake(contentOffset, 0) animated:YES];
            contentOffset = contentOffset + self.view.bounds.size.width;
            // else start sliding form 1
        } else {
            contentOffset = 0;
            [scrollView setContentOffset:CGPointMake(contentOffset, 0) animated:YES];
        }
    }

     

    5. Don’t forget to set contentOffSet in viewDidLoad
     

      - (void)viewDidLoad {
        [super viewDidLoad];
        contentOffset = 0;
        // Do any additional setup after loading the view.
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: