子视图中未识别手势

时间:2012-08-25 20:03:52

标签: objective-c ios

我正在尝试识别子视图中的手势。我只在superview中定义gestureRecognizer时得到响应。我想知道为什么我在子视图中定义它时没有得到gestutre响应,在我的情况下是#View; m',虽然我在superview中定义了gestureHandler时得到了响应我的情况是ViewController的视图,但我想知道它为什么不在子视图中工作。该准则将使其更加清晰。此外,在动画期间无法识别手势,因为在我的情况下,我在我的视图中将子视图从左向右移动.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

1 个答案:

答案 0 :(得分:3)

尝试设置v.userInteractionEnabled = YES