3

closeShareViewユーザーが をタップしたときに呼び出されcontentViewたくありませんmodalView

UIWindow* mainWindow = (((AppDelegate *)[UIApplication sharedApplication].delegate).window);

// Modal view
UIView *modalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
modalView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];

// Content view (goes inside modalView)
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(modalView.center.x, modalView.center.y, 220, 220)];
[modalView addSubview:contentView];
[mainWindow addSubview:modalView];

// Tap gesture (added to modalView)
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeShareView:)];
gr.numberOfTapsRequired = 1;
[modalView addGestureRecognizer:gr];
4

5 に答える 5

3

では、「コンテンツ ビューの外をクリックしてモーダルを閉じる」ような取引が必要ですか?

モーダル ビュー全体にタップ ジェスチャを追加する代わりに、それを処理するバックグラウンド ビューを用意すると、コンテンツ ビューは (ユーザー操作が有効になっている場合) すべてのタップを自然にインターセプトするため、基本的にコーディングを行う必要はありません。

UIWindow* mainWindow = (((AppDelegate *)[UIApplication sharedApplication].delegate).window);

// Modal view
UIView *modalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Background tap view
UIView *backgroundView = [[UIView alloc] initWithFrame:modalView.bounds];
backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];
[modalView addSubview:backgroundView];

// Content view (goes inside modalView)
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(modalView.center.x, modalView.center.y, 220, 220)];
[modalView addSubview:contentView];
[mainWindow addSubview:modalView];

// Tap gesture (added to backgroundView)
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeShareView:)];
gr.numberOfTapsRequired = 1;
[backgroundView addGestureRecognizer:gr];
于 2013-07-14T02:19:17.903 に答える
1

タッチされたビューが自己であるかどうかを確認しているだけです。それ以外の場合はサブビューであり、ジェスチャーを開始しないでください。

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    let point = gestureRecognizer.locationInView(self.view)
    if let viewTouched = self.view.hitTest(point, withEvent: nil) where viewTouched != self.view {
        return false
    } else {
        return true
    }
}
于 2016-03-29T18:16:58.660 に答える