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

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クラスを使用し、Delegateメソッド呼び出しを取得する方法は次のとおりです

ViewController.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 に答える