【问题标题】:How to detect touch over a UIView & detect which UIView is clicked similar as touch over a UIButton?如何检测 UIView 上的触摸并检测单击哪个 UIView 类似于 UIButton 上的触摸?
【发布时间】:2015-05-07 10:33:24
【问题描述】:

我想检测UIView 上的触摸并检测哪个UIView 被点击,类似于触摸 UIButton?你们能解释一下为什么要给负分而不是只给负分。我不认为这个问题对我有用

iOS Detect tap down and touch up of a UIView

How to detect touch over a UIView when touched over a UIButton?

我们对按钮 - (IBAction)Action_Wallet:(id)sender; 进行了操作 UIView 有类似的类型吗

【问题讨论】:

    标签: ios objective-c iphone uiview uibutton


    【解决方案1】:

    这是一个简单的例子。

    - (void)viewDidLoad 
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
    
        float y=50;
    
        for (int i=1; i<8; i++)
        {
            UIView *vwDay=[[UIView alloc]init];
            [vwDay setFrame:CGRectMake(16, y, 252, 45)];
            [vwDay setBackgroundColor:[UIColor brownColor]];
            [vwDay setUserInteractionEnabled:YES];
            [vwDay setTag:2000+i];
    
            UITapGestureRecognizer *tappedOnDay=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappedOnDay:)];
            [vwDay addGestureRecognizer:tappedOnDay];
    
            [self.view addSubview:vwDay];
    
            y=y+50;
    
        }
    
    }
    
    -(void)tappedOnDay:(UITapGestureRecognizer*)recognizer
    {
    
        UIView *vwDay=(UIView*)recognizer.view;
    
       NSLog(@"Tapped on view: %ld",vwDay.tag-2000);
    
    }
    

    在这里,我准备了 7 个视图,并为每个视图提供了 tapGesture 并仅打印用户点击的视图编号。

    【讨论】:

    • 我们对按钮 - (IBAction)Action_Wallet:(id)sender; 有操作,UIView 是否有类似的类型
    • 不,对于视图,我们没有任何 IBAction。这是人们通常遵循的方式来查看任何操作。我们必须给视图提供手势来执行任何事件。
    猜你喜欢
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多