0

にを追加したら、ieのビューを解析UITapGestureRecognizedするUIViewにはどうすればよいですか。タップ中に背景色を変更しますか?これの目的は、ボタンのクリックを模倣することです。

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];
    }
}
4

2 に答える 2

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);
}
于 2013-03-24T13:36:16.597 に答える
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];
}
于 2013-03-24T13:19:08.663 に答える