Touch in scrollview

Discussion in 'iOS Programming' started by Binju, Mar 10, 2010.

  1. Binju macrumors member

    Jan 31, 2010
    I need to dedect touch in the imageview,When the user swipe the image i need to find which is the current image in the imageview and i want to save it.

    .Pls check the attachment

    Attached Files:

  2. AndyCodez macrumors regular

    Aug 6, 2009
    I did something similar to this, except i decided to use buttons instead of uiimageviews. So you don't really need to worry about detecting touch on the object. Then basically I added the button with the scroll view each button had the image set to what was needed. Then when you press the button you do something like
    -(ibaction) btnImagePress:(id) sender
           UIButton *btn = (UIButton*) sender;
           self.selectedImage = sender.image;
    or something along those lines, just pulled that from my crappy memory so forgive me lol.
  3. Binju thread starter macrumors member

    Jan 31, 2010
    I wrote the below code for adding images in the button and scroll.

    But the buttons are not scrolling

    - (void)viewDidLoad {
        [super viewDidLoad];
    	[self playaudios];
    	[btnimage setImage:image1 forState:UIControlStateNormal];
    	[scrollView1 setBackgroundColor:[UIColor blackColor]];
    	[scrollView1 setCanCancelContentTouches:YES];
    	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;
    	NSLog(@" ImageLoad&&&&&&&&&&&&&");
    	for (i = 1; i <= kNumImages; i++)
    		NSString *imageName = [NSString stringWithFormat:@"movimg%d.png", i];
    		UIImage *image1 = [UIImage imageNamed:imageName];
    		UIButton *btnimage = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    		[btnimage setImage:image1 forState:UIControlStateNormal];
    		//imageView = [[UIImageView alloc] initWithImage:image];
    		//[imageView setUserInteractionEnabled:YES];
    		[scrollView1 setUserInteractionEnabled:YES];
    		// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    		CGRect rect = btnimage.frame;
    		rect.size.height = kScrollObjHeight;
    		rect.size.width = kScrollObjWidth;
    		btnimage.frame = rect;
    		btnimage.tag = i;
    		NSLog(@"Image333333333....................");// tag our images for later use when we place them in serial fashion
    		[scrollView1 addSubview:btnimage];
    		[btnimage 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);
  4. AndyCodez macrumors regular

    Aug 6, 2009
    do some calculations to get size needed for all your objects need to be displayed.
    scrollview.contentsize = cgsize();
    i believe that is the proper code. (just doing it from the top of my head) Content size will show how far the scrollview will scroll.
  5. marlenez macrumors newbie

    Aug 25, 2008
    Here's an example of a scrollview with buttons...

    Note that the frame is the size of the scrollview itself but the content size is the maximum for the amount of buttons you will be adding - you can make this any size that you want whether you are placing buttons, labels, images, etc. in the scrollview.

    CGRect frame = CGRectMake(0, 0, 320, 60);
    	scrollViewA = [[UIScrollView alloc] initWithFrame:frame];
    	scrollViewA.showsHorizontalScrollIndicator = YES;
    	scrollViewA.clipsToBounds = YES;
    	[scrollViewA setContentSize:CGSizeMake(640, 60)];
    	[self.view addSubview:scrollViewA];

Share This Page