Touch in scrollview

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

  1. Binju macrumors member

    Joined:
    Jan 31, 2010
    #1
    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

    Joined:
    Aug 6, 2009
    #2
    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
    Code:
    -(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

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

    But the buttons are not scrolling


    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	
    	
    	
    	
    	
    	[self playaudios];
    	
    	
    	
    	[btnimage setImage:image1 forState:UIControlStateNormal];
    	[scrollView1 setBackgroundColor:[UIColor blackColor]];
    	[scrollView1 setCanCancelContentTouches:YES];
    	scrollView1.delaysContentTouches=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];
    		//btnimage.image=image;
    		//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"
    		
    		NSLog(@"Image11111111....................");
    		CGRect rect = btnimage.frame;
    		rect.size.height = kScrollObjHeight;
    		rect.size.width = kScrollObjWidth;
    		NSLog(@"Image222222222....................");
    		btnimage.frame = rect;
    		btnimage.tag = i;
    		NSLog(@"Image333333333....................");// tag our images for later use when we place them in serial fashion
    		[scrollView1 addSubview:btnimage];
    		NSLog(@"Image444444444....................");
    		[btnimage release];
    		NSLog(@"Image555555555....................");
    	}
    	
    	[self layoutScrollImages];	
    }	
    
    
    - (void)layoutScrollImages
    {
    	
    	NSLog(@"layoutScrollImages");
    	UIButton *view = nil;
    	NSArray *subviews = [scrollView1 subviews];
    	NSLog(@"layoutScrollImages1111111111111");
    	// reposition all image subviews in a horizontal serial fashion
    	CGFloat curXLoc = 0;
    	
    	NSLog(@"layoutScrollImages222222222222222");
    	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);
    		}
    		
    		NSLog(@"curXLoc.........=%f",curXLoc);
    	}
    }
    
    
     
  4. AndyCodez macrumors regular

    Joined:
    Aug 6, 2009
    #4
    do some calculations to get size needed for all your objects need to be displayed.
    Code:
    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

    Joined:
    Aug 25, 2008
    #5
    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.


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

Share This Page