【问题标题】:Want to dismiss UIPickerView in UIView while touching outside想要在触摸外部时关闭 UIView 中的 UIPickerView
【发布时间】:2011-09-21 05:29:15
【问题描述】:

我试图通过 UIPickerView 将其关闭,只需触摸视图外的 UIView。我看过像this one 这样的其他帖子,它解释了如何在键盘外按下时使用 NotificationCenter 来关闭 UIKeyboard。我想知道是否有类似的方法可以用 UIPickerView 做同样的事情。现在我在 UIActionSheet 中使用了一个完成按钮,但我喜欢让用户选择在视图外部单击的想法。 另外,我知道使用“隐形按钮”的想法,它的工作方式大致相同,我只是想看看是否有更优雅的解决方案。
提前致谢。

【问题讨论】:

  • 这个问题有几个答案,包括一个here
  • 我知道自定义按钮不是最优雅的,但它是最容易做到的,而不是浪费您宝贵的时间尝试寻找不同的解决方案。用户体验才是真正重要的,他们不会注意到差异。

标签: iphone objective-c uipickerview nsnotificationcenter


【解决方案1】:

我过去通过创建自定义 UIView 然后覆盖 UIView 类的 pointInside 方法来完成此操作。这是每次 UIView 收到触摸通知时都会触发的方法。发生这种情况时,您可以确定触摸是在您的视图范围之内还是之外。

例如,您的自定义 UIView 可能如下所示:

CustomTouchUIView.h

#import 

@protocol CustomTouchUIViewDelegate

- (void) uiViewTouched:(BOOL)wasInside;

@end

@interface CustomTouchUIView : UIView 

// Properties
@property (nonatomic, assign) id delegate;

@end

CustomTouchUIView.m

#import "TOTouchUIView.h"

@implementation CustomTouchUIView

#pragma mark - Synthesize
@synthesize delegate;

#pragma mark - Touches
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if( point.x > 0 && point.x < self.frame.size.width && point.y > 0 && point.y < self.frame.size.height )
    {
        [delegate uiViewTouched:YES ];
        return YES;
    }

    [delegate uiViewTouched:NO ];
    return NO;
}
@end

我有一个可下载的在线示例/教程here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多