0

I,

I'm currently trying to implement a Gesture recognizer into a ScrollView.

I first created a custom ScrollView in which I integrated ImageView object.

When the user clicks on a ImageView, normally the PanGestureRecognizer activates and the ImageView object follow the move on the screen.

I have read and followed the instructions on Gesture Recognizer and the Raywenderlich blog (which is very well done).

If someone has a clue of what is missing in my code, I would be happy to read it

Thank in advance. Here is my code

#import <Foundation/Foundation.h>
#import "mainInterface03.h"
#import <QuartzCore/QuartzCore.h>
#import "boutonHome.h"
#import "DragGestureRecognizer.h"

@class boutonHome;
@class DragGestureRecognizer;

@interface TapScrollView : UIScrollView {

   // id<TapScrollViewDelegate> delegate;
    NSMutableArray *classementBoutons;
    int n;
    int o;
    UIView *bouton01;

}

@property (nonatomic, retain) UIView *bouton01;

@property (retain, nonatomic) IBOutletCollection(UIButton) NSMutableSet* buttons;

-(id)init;
-(void)initierScrollView;

-(void) createGestureRecognizers;
-(IBAction)handlePanGesture:(UIPanGestureRecognizer*)sender;


@end

m.file

#import "TapScrollView.h"


@implementation TapScrollView


@synthesize bouton01;


- (id) init 
{
    if (self = [super init])
    {
        NSLog(@"Classe TapScrollView initiée");
    }
    return self;
}


-(void)initierScrollView
{
    int i;
    for (i=0; i<6; i++) {

        UIImage *image = [UIImage imageNamed:@"back.png"];
        UIImageView *bouton = [[UIImageView alloc] initWithImage:image];
        [bouton setTag:i];
        [bouton setFrame:CGRectMake(72*i+20,10,62,55)];
        [classementBoutons insertObject:bouton atIndex:i];
        [self addSubview:bouton];
        }

        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:bouton01 action:@selector(handlePanGesture:)];
        [bouton01 addGestureRecognizer:recognizer];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    UITouch *touch = [touches anyObject];
    [super touchesBegan:touches withEvent:event];

    for (o=1; o<6; o++) {
    if ([touch view] ==  [self viewWithTag:o]) 
    {
    bouton01 = [self viewWithTag:o];
    }
    }

    return;
}



-(IBAction)handlePanGesture:(UIPanGestureRecognizer*)recognizer
{
    NSLog(@"Mouvement ok");
    CGPoint translation = [recognizer translationInView:self];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                         recognizer.view.center.y + translation.y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self];

}
@end
4

1 に答える 1

0

この設定が機能するかどうかはわかりません。bouton01基本的に、ジェスチャ認識機能を備えた、タッチされたビューを割り当てます。私には少し複雑なようです、そしてまたあなたのコードはそれほど効率的ではありません。

呼び出す[super touchesBegan:touches withEvent:event];と、ビュー階層のタッチアップが渡されるようです。その後にのみ、に割り当てを行ってbouton01ください。したがってbouton01、タッチイベントを決して受け取らないのは論理的に思えます。

確かに、このエラーが発生したのは、ビューを反復処理し、認識機能を持つビューを割り当てるというこの奇妙なアプローチによるものです。セットアップ中に、関係するすべてのビューに同じレコグナイザーを割り当てることをお勧めします。

于 2012-06-24T13:31:41.547 に答える