0

一世,

我目前正在尝试将手势识别器实现到 ScrollView 中。

我首先创建了一个自定义 ScrollView,在其中集成了 ImageView 对象。

当用户单击 ImageView 时,通常 PanGestureRecognizer 会激活,并且 ImageView 对象会跟随屏幕上的移动。

我已阅读并遵循 Gesture Recognizer 和 Raywenderlich 博客上的说明(做得很好)。

如果有人知道我的代码中缺少什么,我很乐意阅读

预先感谢。这是我的代码

#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.文件

#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 回答