2013-03-24 27 views
0

添加UITapGestureRecognizedUIView後,我該如何解析ie的視圖。在水龍頭期間改變背景顏色?這樣做的目的是模仿按鈕點擊。用UIGestureRecognined模仿按鈕點擊

UIView *locationView = [[UIView alloc] init]; 
locationView.tag = 11; 
locationView.backgroundColor = [UIColor clearColor]; 
locationView.userInteractionEnabled = YES; 
[locationView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(promptForLocation)]]; 

比方說,我想locationView.backgroundColor = [UIColor blueColor]後輕按手勢。你只是將其實施在目標行動中還是針對此具體實施?

更新: 這是靈感來自@我的最終代碼爲0x7FFFFFFF

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // ... 
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetectedLocation:)]; 
    longPress.allowableMovement = 50.0f; 
    longPress.minimumPressDuration = 0.05; 
    UILongPressGestureRecognizer *longPress2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetectedPhoto:)]; 
    longPress2.allowableMovement = 50.0f; 
    longPress2.minimumPressDuration = 0.05; 
    [leftView addGestureRecognizer:longPress]; 
    [rightView addGestureRecognizer:longPress2]; 
    // ... 
} 

- (BOOL)longPressDetected:(UILongPressGestureRecognizer *)sender { 
    if ([self.view hasFirstResponder]) { 
     return NO; 
    } 
    if (sender.state == UIGestureRecognizerStateBegan) { 
     [sender.view setBackgroundColor:[UIColor colorWithRed:(4/255.0) green:(129/255.0) blue:(241/255.0) alpha:1]]; 
    } else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed) { 
     [sender.view setBackgroundColor:[UIColor clearColor]]; 
    } 
    CGPoint location = [sender locationInView:sender.view]; 
    return sender.state == UIGestureRecognizerStateEnded && location.x > 0 && location.x < sender.view.frame.size.width && location.y > 0 && location.y < sender.view.frame.size.height; 
} 

- (void)longPressDetectedLocation:(UILongPressGestureRecognizer *)sender { 
    if ([self longPressDetected:sender]) { 
     [self promptForLocation]; 
    } 
} 

- (void)longPressDetectedPhoto:(UILongPressGestureRecognizer *)sender { 
    if ([self longPressDetected:sender]) { 
     [self promptForPhoto]; 
    } 
} 

回答

3

考慮你想模仿點擊一個按鈕,我假設你希望視圖在觸摸結束後恢復到原始狀態。爲此,您需要使用UILongPressGestureRecognizer而不是UITapGestureRecognizer

通過輕擊手勢,在觸摸結束之前不會檢測到識別器,因此只要您擡起手指,就可以有效突出顯示視圖。要解決此問題,請使用長按手勢將其minimumPressDuration屬性設置爲0.0。然後在其選擇器中,檢查發送手勢的狀態;如果它剛剛開始,請更改背景顏色,如果已經結束,則恢復爲原始顏色。

下面是一個例子:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 100.0f, 100.0f)]; 
    [myView setBackgroundColor:[UIColor redColor]]; 
    [self.view addSubview:myView]; 

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetected:)]; 
    [longPress setAllowableMovement:50.0f]; 
    [longPress setMinimumPressDuration:0.0]; 
    [myView addGestureRecognizer:longPress]; 
} 

- (void)longPressDetected:(UILongPressGestureRecognizer *)sender { 
    if (sender.state == UIGestureRecognizerStateBegan) { 
     [sender.view setBackgroundColor:[UIColor blueColor]]; 
    }else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed) { 
     [sender.view setBackgroundColor:[UIColor redColor]]; 
    } 
    NSLog(@"%d",sender.state); 
} 
+0

這是一個相當不錯的解決方法。好主意。謝謝。 – 2013-03-25 13:47:36

+0

@CasparAleksanderBangJespers很高興幫助! – 2013-03-25 13:54:26

1
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedMethod:)]; 
    [locationView addGestureRecognizer:gr]; 

這是方法

-(void)tappedMethod:(UIGestureRecognizer *)ge 
{ 
    // write relavent code here; 
    locationView.backgroundColor = [UIColor blueColor]; 
}