-2

I want to detect touch over a UIView & detect which UIView is clicked similar as touch over a UIButton? Can you guys explain why are you giving negative marking rather then only giving negative marking. I didnt thought this question are useful for me

iOS Detect tap down and touch up of a UIView

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

We have got action for button - (IBAction)Action_Wallet:(id)sender; do we have some similar types for UIView

Community
  • 1
  • 1

1 Answers1

2

Here is simple example.

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

}

Here i am preparing 7 views and giving tapGesture to each view & just printing the view number on which the user tapped.

user4261201
  • 2,324
  • 19
  • 26
  • We have got action for button `- (IBAction)Action_Wallet:(id)sender;` do we have some similar types for `UIView` – rijan maskey May 07 '15 at 10:49
  • No, for views we don't have any IBActions. This is the way generally people follow to give any action to view. We have to give gestures to the view to perform any events. – user4261201 May 07 '15 at 10:52