2013-03-24 6 views
0

UITapGestureRecognizedUIView에 추가 한 경우, 즉 뷰를 어떻게 해석해야합니까? 탭 동안 배경색을 변경 하시겠습니까? 이것의 목적은 버튼 클릭을 모방하는 것입니다.모방 버튼을 클릭하여 UIGestureRecognized를 클릭하십시오.

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]을 원한다고 가정 해 봅시다. 목표 액션에 구현할 것인가 아니면 특정 구현이 있습니까?

업데이트 : 이 나의 마지막 코드는

- (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

입니다 터치가 끝나면 뷰가 원래 상태로 되돌아 가고 싶다고 가정합니다. 이렇게하려면 UITapGestureRecognizer 대신 UILongPressGestureRecognizer을 사용해야합니다.

탭 제스처를 사용하면 터치가 끝날 때까지 인식기가 감지되지 않으므로 손가락을 올리 자마자 효과적으로보기가 강조 표시됩니다. 이 문제를 해결하려면 길게 누르는 제스처 인 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

이 꽤 좋은 해결 방법이다 :

다음은 예입니다. 좋은 생각. 감사. –

+0

@CasparAleksanderBangJespers 도움을 주어 기쁘게 생각합니다! –

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

0x7fffffff @ 영감이 나는, 당신은 버튼 클릭을 모방하려는 고려 방법

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