Tuesday, 5 March 2013

Horizontal scrolling buttons in iPhone


   IBOutlet UIScrollView *scrollView1;
    UIButton *button;

const CGFloat kScrollObjHeight = 110;
const CGFloat kScrollObjWidth = 110.0;
const NSUInteger kNumImages = 7;



- (void)viewDidLoad
{
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 0; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:@"pt%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
//UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        
        
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchDown];
        // [button setTitle:@"Show View" forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateNormal];
        //button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
        // [view addSubview:button];
        
        
CGRect rect = button.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
button.frame = rect;
        button.tag=i;
//imageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:button];
//[imageView release];
}
[self layoutScrollImages];
    
    
}



- (void)layoutScrollImages
{
UIButton *view = nil;
NSArray *subviews = [scrollView1 subviews];
    
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc =0;
for (view in subviews)
{
if ([view isKindOfClass:[UIButton class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}




-(void)aMethod:(id)sender{
    
    
    UIButton *but=(UIButton *)sender;
    NSLog(@"Hee %d",but.tag);
    if(but.tag==1){
        
   

        }else if(but.tag==2){
        pp1ViewController * viewController = [[pp1ViewController alloc] initWithNibName:@"pp1ViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:NO];
        
       
    }else if(but.tag==3){
       }    
}


No comments:

Post a Comment