1

I am trying recognize gestures in a subview. I only get response when i define the gestureRecognizer in the superview. I wanted to know why i am not getting the gestutre response when i define it with in the subview which in my case is 'View.m', although i get response when i define the gestureHandler in the superview which in my case is the view of ViewController but i want why it isnt working in subview. The Code Will Make it more clear. Also the gestures arent recognized during Animation as in my case im moving the subview from left to right in my 'view.m' and when i click during animation gestures arent recognized . i have tried UIViewAnimationOptionAllowUserInteraction but still no gestures are recognized.

This is the SubView Class With a TapgesturHandler

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

And 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

This is ViewController.m class

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

this is my 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

4

Just try setting v.userInteractionEnabled = YES

于 2012-08-25T20:31:15.907 回答