2016-07-08 47 views
2

我有一個自定義UIView更改的UIView幀結束觸摸事件

self.commentView = [[[NSBundle mainBundle] loadNibNamed:@"commentView" owner:self options:nil] firstObject]; 
self.commentView.userInteractionEnabled = YES; 
CGRect frame = CGRectMake(0, [[UIScreen mainScreen]bounds].size.height, [[UIScreen mainScreen]bounds].size.width, 52); 
[self.commentView setFrame:frame]; 
[self addSubview:self.commentView]; 

添加一個手勢識別

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapping)]; 
[self.commentView addGestureRecognizer:tap]; 

自定義視圖包含一個UITextView。我註冊鍵盤的通知,並改變我的自定義視圖幀時的鍵盤出現

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardAppeared:) 
              name:UIKeyboardDidShowNotification 
              object:nil]; 

- (void)keyboardAppeared:(NSNotification*)notificationn{ 
    NSDictionary* keyboardInfo = [notificationn userInfo]; 
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue]; 

    CGRect frame = self.commentView.frame; 
    frame.origin.y = (keyboardFrameBeginRect.origin.y - frame.size.height); 

    [UIView animateWithDuration:.1f animations:^{ 
     [self.commentView setFrame:frame]; 
     self.commentView.alpha = 1.0f; 
    }]; 

    keyboard = keyboardFrameBeginRect.origin.y; 

} 

在我的「竊聽」的方法我有一個NSLog。當我最初添加commentView並點擊視圖時,觸摸工作和tapping被調用。在顯示鍵盤並且將commentView的框架移動到鍵盤上方之後,不再調用「敲擊」 - 觸摸事件停止。

我還有一個關於何時解除鍵盤的通知。在這種方法中,我將框架設置回原始。觸摸重新開始工作。

編輯 這就是我如何添加commentView。上述addSubview:是測試:

- (void) showCommentView{ 
    CGRect frame = CGRectMake(0, [[UIScreen mainScreen]bounds].size.height-52, [[UIScreen mainScreen]bounds].size.width, 52); 
    [self.commentView setFrame:frame]; 
    if (![self.subviews containsObject:self.commentView]) { 
     [self addSubview:self.commentView]; 
    } 
    [self.commentView.commentTextView becomeFirstResponder]; 

    CGRect framer = [self convertRect:self.commentView.postButton.frame fromView:self.commentView]; 
    NSLog(@"SUPERVIEW: %f", framer.origin.y); 
} 

我註釋掉線commentTextView becomeFirstResponder,堆工程。如果我離開這條線,則調用keyboardAppeared,並且一旦更改了框架,水龍頭就不再起作用。所以我認爲commentView的框架實際上並沒有改變。

+0

你在使用模擬器嗎?如果你是真的,你可以通過使用顏色混合圖層來檢查你是否在點擊commentView。可能你正在點擊差異視圖這就是爲什麼手勢不被識別? – Joshua

+0

@Joshua不使用模擬器。將視圖設置爲不同的顏色以及帶有SubviewToFront。很確定我正在點擊正確的視圖 – Peter

+0

不想更改.y值,您應該更改textview的高度 –

回答

0

設置commentViewcolor。我想你的commentView框架在鍵盤出現時不能正確調整大小。

+0

視圖的backgroundColor是明亮的橙色。我想你是正確的。視圖似乎在鍵盤上移動,但奇怪的是它的框架實際上並沒有改變。 – Peter

+0

@peter嗯......你的問題解決了? –