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
- (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
.
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 :)
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..