Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

umaid123

macrumors newbie
Original poster
Nov 9, 2010
1
0
When I scroll so my UIScrollView doesn't set to fixed place and when I pop the view and comes back so it wont scroll also. Please assist me where I am lacking in the code, but when I use left and right arrows so it works absolutely fine and on proper place. It is also not synchronize with pageControl but works synchronized when I use left and right arrows. I want to implement swipe feature for both side, so please assist me on it. Commented code in scrollViewBeginDragging make my scrollview swipe forward only.

Code:
- (void)viewDidLoad {
    [super viewDidLoad];

    self.scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(44, 110, 221.0, 290.0)] autorelease];
    self.scrollView.contentSize = CGSizeMake(NPAGES * 320.0f, scrollView.frame.size.height);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    [self.scrollView setUserInteractionEnabled:YES];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.scrollView setBounces:YES];

    pageControl.numberOfPages = NPAGES;
    pageControl.currentPage = 0;
    [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

    leftimageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftimageBtn.frame = CGRectMake(14, 205, 37, 37);
//leftimageBtn.backgroundColor = [UIColor blackColor];
    [leftimageBtn setBackgroundImage:[UIImage imageNamed:@"arrow_left.png"] forState:UIControlStateNormal];
    [leftimageBtn addTarget:self action:@selector(LeftMove) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftimage];
    [leftimageBtn setHidden:NO];

    rightimageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightimageBtn.frame = CGRectMake(270, 205, 37, 37);
    [rightimageBtn setBackgroundImage:[UIImage imageNamed:@"arrow_right.png"] forState:UIControlStateNormal];
    [rightimageBtn addTarget:self action:@selector(RightMove) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightimage];
    [rightimageBtn setHidden:NO];

    NSArray *videoArr = [preferences Videos:@"HealthHistoryDemoLisa"];
    NSString *imageStr = [videoArr objectAtIndex:1];
    bottomLabelstr = [videoArr objectAtIndex:2];
    videoURL = [videoArr objectAtIndex:0];

//NSLog(@" image %@",imageStr);

    class2Arr = [[NSMutableArray alloc] init];
    [class2Arr addObject:bottomLabelstr];
    [class2Arr addObject:@"Test Content1"];
    [class2Arr addObject:@"Test Content2"];
    [class2Arr addObject:@"Test Content3"];
    [class2Arr addObject:@"Test Content4"];
    [class2Arr addObject:@"Test Content5"];
    [class2Arr addObject:@"Test Content6"];

    UIImageView *videoImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageStr]];
    videoImg.frame = CGRectMake(2.0, 6.0, 231.0, BASEHEIGHT);  
    [self.scrollView addSubview:videoImg];
    [videoImg release];

    UILabel *bottomLbl = [[UILabel alloc] initWithFrame:CGRectMake(4.0, 230.0, 235.0, 60)];
    bottomLbl.textColor = [UIColor whiteColor];
    bottomLbl.backgroundColor = [UIColor clearColor];
    bottomLbl.text = @"Test Content1";
    bottomLbl.font = [UIFont    fontWithName:@"Helvetica" size:15.0];
    bottomLbl.textAlignment = UITextAlignmentCenter;
    bottomLbl.numberOfLines = 2;
    [self.scrollView addSubview:bottomLbl];
    [bottomLbl release];

    imagePlayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imagePlayBtn.frame = CGRectMake(2.0, 6.0, 231.0, BASEHEIGHT);
//[imagePlayBtn setBackgroundImage:[UIImage imageNamed:@"arrow_page_popup.png"] forState:UIControlStateNormal];
    imagePlayBtn.backgroundColor = [UIColor clearColor];
    [imagePlayBtn addTarget:self action:@selector(playMovieAtURL:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:imagePlayBtn];

    NSString *filename = [NSString stringWithFormat:@"TestContent1"];
    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
    iv.frame = CGRectMake(320.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [scrollView addSubview:iv];
    [iv release];

    playerButtonImage1 = [UIButton buttonWithType:UIButtonTypeCustom];
    playerButtonImage1.frame = CGRectMake(415.0, 65.0, 37.0, 37.0);
    [playerButtonImage1 setBackgroundImage:[UIImage imageNamed:@"player_button_play.png"] forState:UIControlStateNormal];
    [playerButtonImage1 setBackgroundImage:[UIImage imageNamed:@"player_button_play_over.png"] forState:UIControlStateHighlighted];
    [playerButtonImage1 addTarget:self action:@selector(SlideShow:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:playerButtonImage1];
    [self.scrollView bringSubviewToFront:playerButtonImage1];


    UILabel *healthLabel = [[UILabel alloc]initWithFrame:CGRectMake(320+4.0, 230.0, 235.0, 40)];
    healthLabel.textColor = [UIColor whiteColor];
    [healthLabel setBackgroundColor:[UIColor clearColor]];
    healthLabel.text = [class2Arr objectAtIndex:1];
    healthLabel.font = [UIFont  fontWithName:@"Helvetica" size:15.0];
    healthLabel.textAlignment = UITextAlignmentCenter;
    healthLabel.numberOfLines = 2;
    [self.scrollView addSubview:healthLabel];
    [healthLabel release];


    NSString *filename1 = [NSString stringWithFormat:@"2b.png"];
    UIImageView *iv1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename1]];
    iv1.frame = CGRectMake(640.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [scrollView addSubview:iv1];
    [iv1 release];

    playerButtonImage = [UIButton buttonWithType:UIButtonTypeCustom];
    playerButtonImage.frame = CGRectMake(735.0, 65.0, 37.0, 37.0);
    [playerButtonImage setBackgroundImage:[UIImage imageNamed:@"player_button_play.png"] forState:UIControlStateNormal];
    [playerButtonImage setBackgroundImage:[UIImage imageNamed:@"player_button_play_over.png"] forState:UIControlStateHighlighted];
    [playerButtonImage addTarget:self action:@selector(SlideShow:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:playerButtonImage];
    [self.scrollView bringSubviewToFront:playerButtonImage];


    UILabel *healthLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(640+2.0, 230.0, 235.0, 40)];
    healthLabel1.textColor = [UIColor whiteColor];
    [healthLabel1 setBackgroundColor:[UIColor clearColor]];
    healthLabel1.text = [class2Arr objectAtIndex:2];
    healthLabel1.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    healthLabel1.textAlignment = UITextAlignmentCenter;
    healthLabel1.numberOfLines = 2;
    [self.scrollView addSubview:healthLabel1];
    [healthLabel1 release];


    NSString *filename2 = [NSString stringWithFormat:@"2c.png"];
    UIImageView *iv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename2]];
    iv2.frame = CGRectMake(960.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [scrollView addSubview:iv2];
    [iv2 release];

    vegetablebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    vegetablebtn.frame = CGRectMake(960.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [vegetablebtn addTarget:self action:@selector(playMovieAtURL:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:vegetablebtn];


    UILabel *DemoLbl = [[UILabel alloc] initWithFrame:CGRectMake(960+14.0, 230, 200, 40)];
    DemoLbl.textColor = [UIColor whiteColor];
    [DemoLbl setBackgroundColor:[UIColor clearColor]];
    DemoLbl.text = [class2Arr objectAtIndex:3];
    DemoLbl.font = [UIFont  fontWithName:@"Helvetica" size:15.0];
    DemoLbl.textAlignment = UITextAlignmentCenter;
    DemoLbl.numberOfLines = 2;
    [self.scrollView addSubview:DemoLbl];
    [DemoLbl release];


    NSString *filename3 = [NSString stringWithFormat:@"OtherVeggies.png"];
    UIImageView *iv3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename3]];
    iv3.frame = CGRectMake(1280.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [scrollView addSubview:iv3];
    [iv3 release];

    UIButton *btnFilename2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnFilename2.frame = CGRectMake(1280.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [btnFilename2 addTarget:self action:@selector(VegetableView) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:btnFilename2];

    UILabel *sweetVegLbl = [[UILabel alloc] initWithFrame:CGRectMake(1280+14.0, 230, 200, 40)];
    sweetVegLbl.textColor = [UIColor whiteColor];
    [sweetVegLbl setBackgroundColor:[UIColor clearColor]];
    sweetVegLbl.text = [class2Arr objectAtIndex:4];
    sweetVegLbl.font = [UIFont  fontWithName:@"Helvetica" size:15.0];
    sweetVegLbl.textAlignment = UITextAlignmentCenter;
    sweetVegLbl.numberOfLines = 2;
    [self.scrollView addSubview:sweetVegLbl];
    [sweetVegLbl release];

    NSString *filename4 = [NSString stringWithFormat:@"2d.png"];
    UIImageView *iv4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename4]];
    iv4.frame = CGRectMake(1600.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [scrollView addSubview:iv4];
    [iv4 release];

    UIButton *btnFilename3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnFilename3.frame = CGRectMake(1600.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [btnFilename3 addTarget:self action:@selector(GloriousView) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:btnFilename3];

    UILabel *gloriousLbl = [[UILabel alloc] initWithFrame:CGRectMake(1600+14.0, 230, 200, 40)];
    gloriousLbl.textColor = [UIColor whiteColor];
    [gloriousLbl setBackgroundColor:[UIColor clearColor]];
    gloriousLbl.font = [UIFont  fontWithName:@"Helvetica" size:15.0];
    gloriousLbl.text = [class2Arr objectAtIndex:5];
    gloriousLbl.textAlignment = UITextAlignmentCenter;
    gloriousLbl.numberOfLines = 2;
    [self.scrollView addSubview:gloriousLbl];
    [gloriousLbl release];


    NSString *filename5 = [NSString stringWithFormat:@"2e.png"];
    UIImageView *iv5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename5]];
    iv5.frame = CGRectMake(1920.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [scrollView addSubview:iv5];
    [iv5 release];

    UIButton *btnFilename4 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnFilename4.frame = CGRectMake(1920.0f+2.0, 6.0f, 231.0f, BASEHEIGHT);
    [btnFilename4 addTarget:self action:@selector(ContactUs) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:btnFilename4];

    UILabel *addLbl = [[UILabel alloc] initWithFrame:CGRectMake(1920+14.0, 230, 200, 40)];
    addLbl.textColor = [UIColor whiteColor];
    [addLbl setBackgroundColor:[UIColor clearColor]];
    addLbl.text = [class2Arr objectAtIndex:6];
    addLbl.font = [UIFont   fontWithName:@"Helvetica" size:15.0];
    addLbl.textAlignment = UITextAlignmentCenter;
    addLbl.numberOfLines = 2;
    [self.scrollView addSubview:addLbl];
    [addLbl release];

    [self.view addSubview:scrollView];

    self.popup = @"UP";
}



- (void) pageTurn: (UIPageControl *) aPageControl
{   
    if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }

    whichPage = aPageControl.currentPage;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    scrollView.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);

    [UIView commitAnimations];

    pageControlUsed = YES;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView1 {
    pageControlUsed = NO;

//if (whichPage < NPAGES-1) {
//      
//      whichPage++;
//      [UIView beginAnimations:nil context:NULL];
//      [UIView setAnimationDuration:0.3f];
//      [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//      scrollView.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);
//      pageControl.currentPage = whichPage;
//      [UIView commitAnimations];
//  }

}


// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControlUsed = NO;
}


- (void) scrollViewDidScroll: (UIScrollView *) aScrollView
{
    CGPoint offset = aScrollView.contentOffset;
    pageControl.currentPage = (offset.x / 100.0f);
//} 
//pageControl.currentPage = scrollView.contentOffset;

}




- (void) LeftMove
{
    if (whichPage > 0) {

        whichPage--;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        scrollView.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);
        pageControl.currentPage = whichPage;

        [UIView commitAnimations];
    }
}

- (void) RightMove
{
    if (whichPage < NPAGES-1) {
        whichPage++;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        scrollView.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);
        pageControl.currentPage = whichPage;

        [UIView commitAnimations];
    }

}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.