2012-06-07 19 views
0

對不起,如果我忽略了一個潛在的回答我的問題,但我在使用的UITapGestureRecognizer與子視圖UIViewController類裏面一個UILabel麻煩......的iOS使用UITapGestureRecognizer用的UILabel刷卡到下一個頁面

基本上,我創建了一個具有UILabel和一些其他不相關元素的自定義UIViewController。 但是,此自定義UIViewController位於啓用了分頁的自定義UIScrollView中,並且標籤已被偏移到足以讓您看到下一個UIViewController的標籤。我想要做的是,當用戶觸摸「下一個」標籤時,scrollRectToVisible:animated:方法觸發,實質上在不滾動的情況下切換頁面。 CustomViewController的UILabel位於頂部。

下面是添加UITapGestureRecognizer到CustomViewController的的UILabel當容器UIScrollView的一個示例代碼:

[scrollView addSubview:CustomViewController]; 

- (void) addSubview: (CustomViewController *) view { 
    // create view's frame here... 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fireScrollRectToVisible:view)]; // this is the current problem like a lot of people out there... 
    [view.Label addGestureRecognizer:tap]; 
    // [super addSubview:view.view]; 
} 
- (void) fireScrollRectToVisible: (CustomViewController *) cvc { 
    CGRect frame = CGRectMake(cvc.view.frame.origin.x, cvc.view.frame.origin.y, 320, 480); 
    [scrollView scrollRectToVisible: frame animated:YES]; 
} 

我最初以爲這將是容易的,但是由於@selector不允許你把論點它這令人難以置信。我想我需要的是訪問CustomViewController的框架並設置scrollRectToVisible,但我不知道了...

我試過這個帖子,但我很新的Objective-C和我不完全瞭解hitTest:withEvent: 我假設hitTest:(CGPoint)與視圖的邊界有關係嗎?

UITapGestureRecognizer initWithTarget:action: method to take arguments?

如果任何人都可以點我這將是偉大正確的方向。 任何幫助將不勝感激!

回答

2

我根據你的代碼

- (void) addSubview: (CustomViewController *) view { 
    // create view's frame here... 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fireScrollRectToVisible:)]; // this is the current problem like a lot of people out there... 
    [view.Label addGestureRecognizer:tap]; 
    // [super addSubview:view.view]; 
} 
- (void) fireScrollRectToVisible: (UIGestureRecognizer *) gesture { 
    UIView *view = [gesture.view superview]; 
    CGRect frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 320, 480); 
    [scrollView scrollRectToVisible: frame animated:YES]; 
} 

希望這可以幫助你一些變化。

+0

令人驚歎!這是一個非常簡單的解決方案,我完全忘記了superview的屬性。非常感謝! – Bill

+0

打敗我吧!很好的答案。 – kevboh

+0

@不用客氣。 :) – looyao

0

如果您需要的是包含包含手勢識別標籤視圖的框架,不能你只是做:

gestureRecognizer.view.superview.frame; 

拿到框架?

您的手勢識別的選擇應該是形式

- (void)gestureRecognizerDidFire:(UIGestureRecognizer *)recognizer; 

如果你設置了選擇器@selector(gestureRecognizerDidFire:),您可以在其中訪問視圖屬性識別器會得到自動傳遞的。

+0

謝謝!這工作得很好:) – Bill

相關問題