2012-08-25 116 views
1

我正在嘗試識別子視圖中的手勢。當我在superview中定義gestureRecognizer時,我只得到響應。我想知道爲什麼我沒有得到gestutre響應,當我在子視圖中定義它,在我的情況是'View.m',雖然我得到的響應時,我在superview中定義的gestureHandler在我的情況是視圖的ViewController,但我想爲什麼它不工作在子視圖。該守則將使其更加清晰。此外,在動畫中不會識別手勢,因爲在我的「view.m」中我將子視圖從左向右移動,並且在動畫手勢中單擊時未被識別。我已經嘗試過UIViewAnimationOptionAllowUserInteraction,但仍然沒有手勢被識別。子視圖上未識別的手勢

這是子視圖類與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 

而且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 
+0

如果你不能在你的SuperView已經部分通過是正常的子視圖被重疊水龍頭 - 那是什麼u的觀察? – tiguero

+0

選擇一個好的命名方式:View作爲UIView的子類很蹩腳。根據你的觀點選擇你的名字...... – tiguero

+0

是的,我其實是想弄清楚爲什麼手勢識別器在動畫過程中不工作。作爲即時動畫視圖類,所以我張貼在這裏可能會有人會有答案。 – Azerue

回答

3

只是嘗試設置v.userInteractionEnabled = YES

+0

所有UIViews默認情況下都有userInteractionEnabled不是? – tiguero

+0

如果他是UIView的子類,則爲true,但UIImageView不是這種情況。 –

+0

他是從UIView繼承的,而他的類的名稱是GestrureonImageView ... – tiguero