Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Scrollview doesn't remains on the center indicator

Umaid123Umaid123 Posts: 25Registered Users
I am enclosing my sample code herewith for ready reference, please make it in center place, this is what I am facing problem in. It is not stopping on the center indicator.

Note : All button are dynamic and coming from web, but in this sample I made it from a hard coded array. But in original scenerio their count(number of buttons) may change.

- (void)viewDidLoad {

arr = [[NSMutableArray alloc] initWithObjects:@\"Test1\",@\"Test2\",@\"Test3\",@\"Test4\",@\"Test5\",nil];


UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"bg_content_slide.png\"]];
img.frame = CGRectMake(0, 20, 320, 85);
img.contentMode = UIViewContentModeScaleAspectFill;


[self.view addSubview:img];

[self setScrollButtons];

[super viewDidLoad];

}

- (void) setScrollButtons
{
tagScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 44)];
tagScrollView.backgroundColor = [UIColor clearColor];
[tagScrollView setShowsHorizontalScrollIndicator:NO];
[tagScrollView setShowsVerticalScrollIndicator:NO];
[tagScrollView setCanCancelContentTouches:NO];
tagScrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
tagScrollView.clipsToBounds = YES;
tagScrollView.scrollEnabled = YES;
tagScrollView.pagingEnabled = YES;
tagScrollView.bounces = YES;
tagScrollView.delegate = self;
tagScrollView.tag = 2;

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

int xaxis = 0;

for (int i = 0; i < [arr count]; i++) {

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(xaxis+80, 0, 160, 44);
//btn.titleLabel.text = [NSString stringWithFormat:@\"%@\",[arr objectAtIndex:i]];
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
NSLog(@\"%@\", btn.titleLabel.text);

xaxis += btn.frame.size.width + 40;

[tagScrollView addSubview:btn];
}


[tagScrollView setContentSize:CGSizeMake([arr count] * 220, tagScrollView.frame.size.height)];

[self.view addSubview:tagScrollView];
}

- (void) pageTurn: (UIPageControl *) aPageControl
{
whichPage = aPageControl.currentPage;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
tagScrollView.contentOffset = CGPointMake(200.0f * whichPage, 0.0f);

[UIView commitAnimations];
}

- (void) scrollViewDidScroll: (UIScrollView *) scrollView
{
CGPoint offset = scrollView.contentOffset;

NSLog(@\"offset is %@\",NSStringFromCGPoint(offset));

CGFloat pageWidth = floor(scrollView.contentSize.width/pageControl.numberOfPages) ;
//pageWidth = 190;

int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(@\"Page is : %d\",page);
pageControl.currentPage = page;

}



- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate // called on finger up if user dragged. decelerate is true if it will continue moving afterwards
{
CGFloat pageWidth = floor((scrollView.contentSize.width-220)/pageControl.numberOfPages) ;
//pageWidth = 190;

int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth)+1;
NSLog(@\"Page is : %d\",page);
pageControl.currentPage = page;

}



Find source code here
http://www.4shared.com/file/O2AgReCd/TestingScroll.html
Sign In or Register to comment.