1

サブビューでジェスチャーを認識しようとしています。スーパービューでgestureRecognizerを定義したときにのみ応答が得られます。私の場合は「View.m」であるサブビューで定義すると、ジェスチャ応答が得られない理由を知りたかったのですが、私の場合はビューであるスーパービューでgestureHandlerを定義すると応答が得られますViewControllerのですが、サブビューで機能しない理由が知りたいです。コードはそれをより明確にします。また、私の場合、「view.m」でサブビューを左から右に移動しているように、アニメーション中にジェスチャが認識されず、アニメーション中にクリックするとジェスチャが認識されません。UIViewAnimationOptionAllowUserInteraction を試しましたが、まだジェスチャーが認識されません。

これは TapgesturHandler を持つ SubView クラスです

//  View.m
//  GestrureonImageView
//
//  Created by Noman Khan on 8/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "View.h"

@implementation View

- (id)initWithFrame:(CGRect)frame
{

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }

    UITapGestureRecognizer *tapGestures=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesturesCallBack:)];

    // NSLog(@"init gestures");

    [tapGestures setDelegate:self];

    // Set required taps and number of touches
    [tapGestures setNumberOfTapsRequired:1];
    [tapGestures setNumberOfTouchesRequired:1];
    [self addGestureRecognizer:tapGestures];
    return self;
}
-(void) gesturesCallBack:(UITapGestureRecognizer *)sender 
{
    NSLog(@"abcView");
    [UIView animateWithDuration:10 

                          delay:0.1
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^
     {
         // [self initGestures];
         CGAffineTransform transform = CGAffineTransformMakeTranslation(500,0);

         self.transform = transform;
     }
                     completion:^(BOOL finished){


                     }];   

}



-(void)viewDidLoad{

    NSLog(@"Inview");

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

そして View.h

//  View.h
//  GestrureonImageView
//
//  Created by Noman Khan on 8/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface View : UIView<UIGestureRecognizerDelegate>

@end

これは ViewController.m クラスです

//  ViewController.m
//  GestrureonImageView
//
//  Created by Noman Khan on 8/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize v;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void) gesturesCallBack:(UITapGestureRecognizer *)sender 
{
    NSLog(@"abc");

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor blueColor]];

    UITapGestureRecognizer *tapGestures=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesturesCallBack:)];

   // NSLog(@"init gestures");

    [tapGestures setDelegate:self];

    // Set required taps and number of touches
    [tapGestures setNumberOfTapsRequired:1];
    [tapGestures setNumberOfTouchesRequired:1];

    [self.view addGestureRecognizer:tapGestures];
    v=[[View alloc]initWithFrame:CGRectMake(40, 50, 80, 50)];
    v.backgroundColor=[UIColor yellowColor];
//    [v addGestureRecognizer:tapGestures];    
    [self.view addSubview:v];


    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

これは私のViewController.hです

//  ViewController.h
//  GestrureonImageView
//
//  Created by Noman Khan on 8/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "View.h"

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>


@property(nonatomic,strong)View *v;

@end
4

1 に答える 1