如何检测用户所在的图像对象?
尝试以这种方式这样做,但它无法正常工作
- (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;
}
编辑:如果我这样使用它仍然无法正常工作
- (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);
}
}}
如果我遗失了什么,请告诉我。
由于
答案 0 :(得分:3)
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth);
}
只有当您启用了UIScrollView
的分页时,上述方法才能为您提供正确的当前页码。
答案 1 :(得分:2)
您最好的方法是为Imageview创建一个自定义类,并从UIImageView继承。在该图像上设置一个委托方法,它将响应图像上的委托类这里是代码
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];
}
}
以下是如何在View Controller上使用CustomImageView类并获取委托方法调用
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
我希望这有助于您快乐编码:)
答案 2 :(得分:1)
您可以在scrollview中为所有子视图指定标签,并使用触摸事件检测它,如下所示:
- (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
}
}
希望它对你有用..