iphone - 手势识别器委托(delegate) : how to implement touch delegate functions

标签 iphone uiview delegates uigesturerecognizer

我实际上正在尝试在实现 touchBegan:withEvent 的 customScrollView 类中插入触摸委托(delegate)函数(类型 touchEnded:withEventgestureRecognizers )。 。

当我尝试将识别器对象的委托(delegate)设置为 self 时,我的 SDK 出现一条警告消息,指出“类型 ID 不兼容”。

我知道 GestureRecognizer 的委托(delegate)协议(protocol)不包含此类函数,但我不知道应该触发哪个委托(delegate)才能在自定义 View 中使用上述函数。

非常感谢您的回复

维克多-玛丽

这是我的代码:

@interface TapScrollView : UIScrollView {

  //  id<TapScrollViewDelegate> delegate;
    NSMutableArray *classementBoutons;
    int n;
    int o;
//UIImageView *bouton;

}
//@property (nonatomic, assign) id<TapScrollViewDelegate> delegate;
//@property (nonatomic, retain) UIImageView *bouton;

//@property (strong, nonatomic) UIPanGestureRecognizer *bouton01pan;

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

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


@end



#import "TapScrollView.h"


@implementation TapScrollView

//@synthesize bouton;

- (void)setUpBoutonView {
    // Create the placard view -- its init method calculates its frame based on its image
    //boutonHome *aBoutonMain = [[boutonHome alloc] init];
    //self.boutonMain = aBoutonMain;
    //[boutonMain setCenter:CGPointMake(200, 200)];
    //[self addSubview:boutonMain];
}

- (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+20*i,10,62,55)];
        [classementBoutons insertObject:bouton atIndex:i];
        bouton.userInteractionEnabled = YES;

        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
        recognizer.delegate = self; 
        [bouton addGestureRecognizer:recognizer];
        [self addSubview:bouton];
}
}
//- (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]) 
  // {
  //   UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:[classementBoutons objectAtIndex:o] action:@selector(handlePanGesture:)];
   // [[classementBoutons objectAtIndex:o] addGestureRecognizer:recognizer];
   // bouton01 = [self viewWithTag:o];
   // }
   // }

    //CGPoint touchPoint = [touch locationInView:self];
    //[self animateFirstTouchAtPoint:touchPoint];
  //  return;
//}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (n=0; n<6; n++) {
        NSLog(@"touche cancelled");
        [[classementBoutons objectAtIndex:n] setFrame:CGRectMake((72+20)*n,10,62,55)];
    }


}

//- (id<TapScrollViewDelegate>) delegate {
//   return (id<TapScrollViewDelegate>)super.delegate;
//}

//- (void) setDelegate:(id<TapScrollViewDelegate>) aDelegate 
//{
//    super.delegate = aDelegate;
//}

#define GROW_ANIMATION_DURATION_SECONDS 0.15
#define SHRINK_ANIMATION_DURATION_SECONDS 0.15



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

最佳答案

尝试指定:

@interface TapScrollView : UIScrollView <UIGestureRecognizerDelegate> {

这样,你的警告应该就会消失,尽管我不得不承认我还没有完全清楚你想要完成什么,所以可能还有其他问题。

关于iphone - 手势识别器委托(delegate) : how to implement touch delegate functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11281251/

相关文章:

javascript - WKWebView invalidaccesserror dom异常15底层对象不支持参数或操作

iphone - 从 iPhone 中的音乐文件中获取 NSData

iphone - 路由 iPhone 音频声音

ios - UIActivityIndi​​catorView 在出现在 transitionWithView 中后没有动画

ios - 在模态视图下方旋转 View

swift - 为什么这些 CAShapeLayers 没有到达预期的位置?

android - Eclipse 在尝试启动 Android 应用程序时卡住了

c# - 获取委托(delegate)的所有 Target 实例

c# - 为什么匿名方法可以传递给委托(delegate)者的构造函数?

ios - 在 iOS 中更新用户 A 数据给用户 B