IOS scroll gallery - ios

i made this sliding gallery in my app:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(photoTap:)];
tap.numberOfTapsRequired = 1;
int pageCount = 3;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,
0,
360,
200)];
scrollView.backgroundColor = [UIColor clearColor];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(pageCount*scrollView.bounds.size.width ,
scrollView.bounds.size.height);
[scrollView addGestureRecognizer:tap];
CGRect viewsize = scrollView.bounds;
UIImageView *image = [[UIImageView alloc]initWithFrame:viewsize];
[image setImage: [UIImage imageNamed:#"01.png"]];
image.userInteractionEnabled = YES;
[scrollView addSubview:image];
viewsize = CGRectOffset(viewsize, scrollView.bounds.size.width, 0);
UIImageView *image2 = [[UIImageView alloc]initWithFrame:viewsize];
[image2 setImage: [UIImage imageNamed:#"02.png"]];
image2.userInteractionEnabled = YES;
[scrollView addSubview:image2];
- (void)photoTap:(UITapGestureRecognizer *) gestureRecognizer{
}
How can i detect which photo is tapped in my scrollview?
I have tried to add gesture recognizer to the image but only the last one works.
Any suggestion? Thank You

You can add gesture recognizer to the scrollview, after detecting the tap, detect its location on the UIScrollView's content by adding the coordinates of the tap to the content offset. When you have this coordinate i think that detecting the image is not a problem.

Related

How to Zoom Image on double tap and pinch with 2 finger in iOS?

In my application , used gallery concept. And this want to Zoom Image with double tap and using 2 finger.
I used below code for this but its not working for me. i enable to achieve this functionality..
Please resolve my problem.
- (void)viewDidLoad
{
[self setupScrollView];
}
-(void) setupScrollView
{
//add the scrollview to the view
image_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image_scroll.pagingEnabled = YES;
[image_scroll setAlwaysBounceVertical:NO];
//setup internal views
for (int i = 0; i < [self.PhotoImgArr count]; i++) {
current_page =(int)self.PhotoImgArr[i];
CGFloat xOrigin = i * self.view.frame.size.width;
AsyncImageView *image = [[AsyncImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image.imageURL = [NSURL URLWithString:self.PhotoImgArr[i]];
image.contentMode = UIViewContentModeScaleAspectFit;
[image_scroll addSubview:image];
}
//set the scroll view content size
[image_scroll setShowsHorizontalScrollIndicator:NO];
[image_scroll setShowsVerticalScrollIndicator:NO];
[image_scroll setMaximumZoomScale:8.0];
self.image_view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
image_scroll.decelerationRate = UIScrollViewDecelerationRateFast;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[image_scroll addGestureRecognizer:doubleTap];
image_scroll.contentSize = CGSizeMake(self.view.frame.size.width *
self.PhotoImgArr.count,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:image_scroll];
[image_scroll setZoomScale:[image_scroll minimumZoomScale]];
}
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
if(image_scroll.zoomScale > image_scroll.minimumZoomScale)
[image_scroll setZoomScale:image_scroll.minimumZoomScale animated:YES];
else
[image_scroll setZoomScale:image_scroll.maximumZoomScale animated:YES];
}
You should set userInteractionEnabled of image_scroll
image_scroll.userInteractionEnabled = YES;

Visible text of UITextView in UIScrollView which has subview UIImageView

when typing in the UITextView the text is not visible;
when typing is done the text is not visible.
How to make it visible?
The code is below. I do not put the UITextView related code intentionally as only font and alignment properties are set.
// Setup the background image view.
[self.backgroundImageView setBackgroundColor:[UIColor blackColor]];
[self.view sendSubviewToBack:self.backgroundImageView];
// Set the image view.
CGRect r = CGRectMake(0, 0, self.TextView.bounds.size.width, 455);
ImageView = [[UIImageView alloc] initWithFrame:r];
// Set up the scroll view for the image zooming in/out and scrolling.
imgScrollView = [[UIScrollView alloc] initWithFrame:ImageView.bounds];
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:ImageView];
[imgScrollView setBackgroundColor:[UIColor clearColor]];
[imgScrollView setContentSize:ImageView.frame.size];
imgScrollView.maximumZoomScale = 5.0;
imgScrollView.delegate = self;
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[imgScrollView addGestureRecognizer:doubleTapRecognizer];
[imgScrollView sendSubviewToBack:self.TextView];
[self.view addSubview:imgScrollView];
UPDATE
[imgScrollView addSubview:self.TextView];
the line of code above solved this.

Z position of UIImageView to that of UIScrollView

I have both UIScrollView and another UIImageView. I want the UIImageView to come over the UIScrollView. I've tried all the bringSubviewToFront and the insertAtIndex stuff but its not working. Please help me out!
Here is the code -
For UIScrollView:
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 300,
SCREEN_WIDTH, SCREEN_HEIGHT)];
scrollView.showsHorizontalScrollIndicator = FALSE;
[self.view addSubview:scrollView];
__block int tagValue = 1;
__block NSInteger tag = 1;
for (int i=0; i<[listOfImages count]; i++) {
NSDictionary *myDic = [listOfImages objectAtIndex:i];
NSString *urlImage = [myDic objectForKey:#"product_image"];
//NSLog(#"%lu",(unsigned long)[listOfImages count]);
image = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, 0, 200, 140)];
// [image setImage:[UIImage imageNamed:#"img_def.png"]];
NSURL *imageURL = [NSURL URLWithString:urlImage];
[image setImageWithURL:imageURL
placeholderImage:[UIImage imageNamed:#"img_def.png"]];
image.tag = tag;
image.contentMode = UIViewContentModeScaleAspectFit;
[scrollView insertSubview:image atIndex:1];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[image addGestureRecognizer:recognizer];
[image setUserInteractionEnabled:YES];
leftMargin += SCREEN_WIDTH;
tagValue += 1;
tag += 1;
}
[scrollView setContentSize:CGSizeMake(leftMargin, 0)];
[scrollView setPagingEnabled:YES];
And the image code that I want to come on top of the scroll view -
UIImage *originalPromo = [UIImage imageNamed:#"promo"];
UIImage *scaledPromo = [UIImage imageWithCGImage:[originalPromo CGImage]
scale:(originalPromo.scale *2.0) orientation:(originalPromo.imageOrientation)];
UIImageView *promo = [[UIImageView alloc] init];
[promo setFrame:CGRectMake(45.5, 300.0,
scaledPromo.size.width, scaledPromo.size.height)];
[promo setImage:scaledPromo];
[self.view insertSubview:promo atIndex:100];
Instead of using insertSubview:atIndex use addSubview:. When a subview is added, it's always added on top of all other subviews already in the parent view.

UIScrollView With Pictures Generated

I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

UIscrollview and uibuttons tap

I am using a UIScrollView where I got some UIButtons, to recognize that I pressed the button above, I created a subclass of UIScrollView where rewrite the method
- (BOOL) touchesShouldCancelInContentView (UIView *) view
but only enters that method when the touch is a journey, never comes when I press on the screen without more.
the UIScrollView I've created thus:
`
CGSize containerSize = CGSizeMake(320.0f, 480.0f);
CGSize containerSize2 = CGSizeMake(640.0f, 960.0f);
self.scrollView = [[UIScrollViewWithButtons alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize}];
[self.scrollView setMinimumZoomScale:0.5f];
[self.scrollView setMaximumZoomScale:1.0f];
[self.scrollView setZoomScale:0.5f];
[self.scrollView setDelegate:self];
self.scrollView.bounces = NO;
self.scrollView.bouncesZoom = NO;
self.scrollView.delaysContentTouches = NO;
self.scrollView.userInteractionEnabled = YES;
[self.scrollView setContentInset:UIEdgeInsetsZero];
[self.view addSubview:self.scrollView];
self.containerView = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize2}];
self.containerView.userInteractionEnabled = YES;
[self.scrollView addSubview:self.containerView];
[self.scrollView setClipsToBounds:NO];
//instanciar uimageview
fondo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 640, 960)];
[self.containerView addSubview:fondo];
[fondo setImage:[UIImage imageNamed:#"Grande.png"]];
self.scrollView.contentSize = containerSize2;
and button add
imagen = [UIButton buttonWithType:UIButtonTypeCustom];
[imagen addTarget:self action:#selector(pulsadoIzq) forControlEvents:UIControlEventTouchUpInside];
[imagen setTitle:#"" forState:UIControlStateNormal];
[imagen setFrame:CGRectMake(xUser, yUser, 50.0f, 50.0f)];
[imagen setImage:[UIImage imageNamed:#"boton.png"] forState:UIControlStateNormal];
[self.containerView addSubview:fichaUserIzq];
imagen.center = CGPointMake(xUser, yUser);`enter code here`
I need to capture the tap on the screen to the buttons
How I can do?
I would also like to know because if I have implemented this code:
img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"img.png"]];
UIPanGestureRecognizer *panRecg = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(imgDragged:)];
img.userInteractionEnabled = YES;
[img addGestureRecognizer:panRecg];
img.center = CGPointMake(160, 436);
[self.scrollView addSubview:img];
When I click on the image nothing happens, does not enter the method, only comes when I press and drag. I also want to capture when pressed over this image and so I capture only when the move.
I solved the problem, I implement this method:
-(void)singleTapGestureCaptured:(UITapGestureRecognizer*)gesture{
CGPoint touchPoint = [gesture locationInView:self.scrollView];
if ((touchPoint.x > 88)&&(touchPoint.x < 226)&&(touchPoint.y > 172)&&(touchPoint.y < 319)) {
if (rodando) {
[self pararAnimacionDado];
}
}
}
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];

Resources