This is the image from app store whenever we search for any app. I also want to add the same scrollview concept in which it show the currentimage with small preview of previous and next image. I am able to make this view with the help of Sample code. But didn't find any solution that how to get index or tagvalue when user tap any of the image. So that I would be able to open the detail page for each image. If Someone has idea about this please help me.
Thanks in advance.
Add gesture recognizer to the necessary image view:
UITapGestureRecognizer *frameTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frameTapGesture:)];
[_imgView addGestureRecognizer:frameTapGesture];
Then in the gesture handler get access to the view gesture recognizer attached to:
- (void)frameTapGesture:(UITapGestureRecognizer*)sender
{
UIView *view = sender.view; //cast pointer to the derived class if needed
NSLog(@"%d", view.tag);
}
At the risk of making a recommendation without seeing the full picture of your app, why not use a custom UIButton instead of UIImageViews for this? With a UIButton you can setup an action and pass the sender id, from which you can easily access your tags and pull the data from your array.
UIButton *button = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)];
button.backgroundColor = [UIColor blueColor];
button.tag = i;
[button addTarget:self action:@selector(touchDown:) controlEvent:UIControlEventTouchDown];
And inside of the touchDown: method you simply have to cast the sender to a UIButton in order to access the tag
- (void)touchDown:(id)sender
{
UIButton* button = (UIButton*)sender;
switch(button.tag)
{
case TAG1:
break;
//etc
}
}
OR check this out
UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame: CGRectMake (0,100,200,30)];
NSMutableArray = *images = [NSMutableArray alloc] initWithObjects: img1,img2,img3,nil];
for (int i=0; i<3; i++)
{
// set `imageView` frame
UIImageView *imageV = [[UIImageView alloc]initWithFrame:yourFrame];
[imageV setImage:[images objectAtIndex:i]];
[imageV setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector (getTag:)];
[imageV addGestureRecognizer: singleTap];
[myScroll addSubview:imageV];
}
set tag to images as per you want
After adding all imageView successfully you get them click like this :-
-(void)getTag:(id)sender
{
UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender;
UIImageView *imageView = (UIImageView *)recognizer.view;
if(imageView.image.tag==1)
{
[imageView setImage:[UIImage imageNamed:@"anyImage.png"]];
}
}
The Best way to achieve what you wish is to add a UITapGesture to your image:
let tapgesture = UITapGestureRecognizer(target: self, action: Selector("openImage:"))
tapgesture.numberOfTapsRequired = 1
//if you're using a collectionView or tableview add this code inside cellForItemAtIndexPath
cell.Img.addGestureRecognizer(tapgesture)
//otherwise
myImage..addGestureRecognizer(tapgesture)
Get the superview of your tap gesture, this will give you the correct indexPath. Try this:
func openImage(sender: UITapGestureRecognizer)
{
let point = sender.view
let mainCell = point?.superview
let main = mainCell?.superview
let cell: myViewCell = main as! myViewCell
let indexPath = collectionView.indexPathForCell(cell)
}
You can increase or reduce the superview's depending on you hierarchy level. Start from one superview and keep increasing till you get the indexPath
Suppose you have an array with the objects to display and a UIImageView
to display the image, just do this when setting up the view with the index i
:
imageView.tag = kImageTag + i;
where kImageTag
is any constant > 1 to make sure you don't get a 0 as the tag.
Now when the view is selected you can simply check for the tag of the image view.
I have resolved the issue and really thanks to all to look upon my issue.
For those using storyboards
and swift
:
Change tag of every UIView or UIImageView in IB (interface builder) ex:
view1.tag = 1
view2.tag = 2
view3.tag = 3
...
- Still in storyboard add UITapGestureRecognizer to every UIView you have put in storyboard view (view1, view2, view3 ...) :
- Write your function in Swift file:
// code in swift file :
@IBAction func viewTapped(sender: AnyObject) {
// You can write anything here. I'm guessing you need something with 'enable/disable' function, and the best call is the CustomView which you create from File > New file
print("tapped view is view with tag: \(sender.view!!.tag)")
}
- Connect every TapGestureRecognizer to same function you have declared in parent UIViewController swift file:
- Done.