0

How can i detect which image object user is on?

Trying to do it this way but it is not working

 - (void)scrollViewDidScroll:(UIScrollView *)sender {

// [_imageScrollView contentOffset];

CGFloat imageViewWidth = _imageScrollView.frame.size.width;
//int currentPage = floor((_imageScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSInteger image = (NSInteger)floor((self.imageScrollView.contentOffset.x * 2.0f + imageViewWidth) / (imageViewWidth * 2.0f));
[_imageScrollView contentOffset];

//self.imageView.image = image;
}

EDIT: If i use it like this still it is not working

- (void)LongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
    CGPoint location = [gesture locationInView:_imageView];
    CGFloat imageViewWidth = _imageScrollView.frame.size.width;
    int imageView = floor((_imageScrollView.contentOffset.x - imageViewWidth / 2) / imageViewWidth) + 1;

    NSLog(@"contains point?? - %d", CGRectContainsPoint(_imageView.bounds, location));


    if (CGRectContainsPoint(_imageView.bounds, [self.view convertPoint:location toView:_imageView]))
    {

        UIImageWriteToSavedPhotosAlbum(_image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
    }
}}

Please let me know if i m missing something.

Thanks

user1452248
  • 767
  • 2
  • 11
  • 28

3 Answers3

3
- (void)scrollViewDidScroll:(UIScrollView *)sender 
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth);
}

Above method give you correct current page number, only when you have enabled the paging of UIScrollView.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
Rajneesh
  • 81
  • 4
1

You can assign tags to all subviews in scrollview and detect it using touch event as follows:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // assign a UITouch object to the current touch
    UITouch *touch = [[event allTouches] anyObject];

    // if the view in which the touch is found is your image

    if ([[touch view]tag] == imageObj.tag) {
        // do stuff here
    }
}

Hope it works for you..

iCreative
  • 1,499
  • 1
  • 9
  • 22
  • for touch i m using longpressgesture – user1452248 Oct 20 '12 at 16:14
  • Can you get UIView Object on which you are performing longPressGuesture??? If you can, then it'll work for that too. Only the delegate method will change. Either, out this method too in your code & try. – iCreative Oct 20 '12 at 16:16
  • You gave me good idea now i m going to try with long press gesture to see which image object user is currently on. Let me see if it works. Because one handlelongpress i m using for showing action sheet which works very well – user1452248 Oct 20 '12 at 16:26
  • @user1452248 hey have a look of `abhishekkhrawar` answer it's pretty good. – Kamar Shad Oct 20 '12 at 20:59
1

Hi the best way to do this is create a custom class for Imageview and inherit if from UIImageView Set a delegate method on tap on that image and it will respond to delegate class on tap on image Here is the code

CustomImageView.h

#import <UIKit/UIKit.h>

@protocol CustomImageDelegate;

@interface CustomImageView : UIImageView

@property (nonatomic, retain) id <CustomImageDelegate> delegate;

@end


@protocol CustomImageDelegate <NSObject>

- (void)customImageTapped:(CustomImageView*)customImageView;

@end

CustomImageView.m

#import "CustomImageView.h"

@implementation CustomImageView

@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    if(_delegate && [_delegate respondsToSelector:@selector(customImageTapped:)])
    {
        [_delegate customImageTapped:self];
    }
}

Here is how to use CustomImageView Class on View Controller And Get Delegate Method Call

ViewController.h

#import <UIKit/UIKit.h>

#import "CustomImageView.h"


@interface ViewController : UIViewController <CustomImageDelegate>


@end

ViewController.

#import "ViewController.h"

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    // Here create your scrollview and add on self.view

   // create objects of customimageview and set image and add all customimageviews on scrollView



}

-(void)customImageTapped:(CustomImageView *)customImageView{

    //On Tap this method will call and you can get image here by using customImageView.image

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

i hope this will helpful for you happy coding :)

abhishekkharwar
  • 3,529
  • 2
  • 18
  • 31
  • ImageView is recognizing the long press gesture. Real problem i m having is that it is not recognizing on which image object user is long pressing it is always saving last image. So i want to know what should i do track the real image object on which user tapped. – user1452248 Oct 20 '12 at 21:28
  • Can you please add your long press gesture code here where you are adding long press gesture ?? – abhishekkharwar Oct 21 '12 at 04:33
  • @user1452248 .you just need to change the way of detecting the click over ImageView in UIScrollView just modify your code little bit .hey you should use above code it's clear .Above Answer use TouchEvent method instead of long press gesture.For This Set the Tag to each UIImageView Object at time of creation of That UIIMageView. – Kamar Shad Oct 21 '12 at 08:07