0

如何检测用户在哪个图像对象上?

试图这样做,但它不工作

 - (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);
    }
}}

如果我遗漏了什么,请告诉我。

谢谢

4

3 回答 3

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

上面的方法给你正确的当前页码,只有当你启用分页时UIScrollView

于 2012-10-20T17:22:52.013 回答
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

自定义图像视图.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];
    }
}

以下是如何在视图控制器上使用 CustomImageView 类并获取委托方法调用

视图控制器.h

#import <UIKit/UIKit.h>

#import "CustomImageView.h"


@interface ViewController : UIViewController <CustomImageDelegate>


@end

视图控制器。

#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

我希望这对你编码快乐有帮助:)

于 2012-10-20T20:42:42.287 回答
1

您可以为滚动视图中的所有子视图分配标签并使用触摸事件检测它,如下所示:

- (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
    }
}

希望这对你有用..

于 2012-10-20T16:12:14.373 回答