ios - How can I add swipe gestures to 3 different UILabels? -
i'm new xcode - objective-c , in first app need add 3 different labels 2 swipe gestures (up , down swipe).
to more clear (for cannot add images) have 3 labels this:
[label1].[label2][label3]
min value labels '0' max value label1 '2', label2 , label3 '9' each one.
when swipe down labels need decrease value , when swipe need decrease value.
how can this? in example made before did works 1 single uilabel.
thanks lot in advance. regards.
you need initialise 3 swipes, each label swipe itself. propose using tags differ labels.
here code creates labels programmatically:
uilabel *label1 = [[uilabel alloc] initwithframe:cgrectmake(50, 50, 100, 100)]; label1.text = @"label 1"; label1.tag = 10; label1.userinteractionenabled = yes; uiswipegesturerecognizer *swipe1 = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(didswipelabel:)]; [label1 addgesturerecognizer:swipe1]; uilabel *label2 = [[uilabel alloc] initwithframe:cgrectmake(50, 150, 100, 100)]; label2.text = @"label 2"; label2.tag = 20; label2.userinteractionenabled = yes; uiswipegesturerecognizer *swipe2 = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(didswipelabel:)]; [label2 addgesturerecognizer:swipe2]; uilabel *label3 = [[uilabel alloc] initwithframe:cgrectmake(50, 250, 100, 100)]; label3.text = @"label 3"; label3.tag = 30; label3.userinteractionenabled = yes; uiswipegesturerecognizer *swipe3 = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(didswipelabel:)]; [label3 addgesturerecognizer:swipe3]; [self.view addsubview:label1]; [self.view addsubview:label2]; [self.view addsubview:label3];
the swipe method:
- (void)didswipelabel:(id)sender { // parse swipe gesture uiswipegesturerecognizer *swipe = sender; // view called swipe uilabel *swipedlabel = (uilabel *)swipe.view; // switch tag switch (swipedlabel.tag) { case 10: nslog(@"swiped label 1"); break; case 20: nslog(@"swiped label 2"); break; case 30: nslog(@"swiped label 3"); break; default: break; } }
if prefer use interface builder, can accomplish setting view's tag in "attributes inspector" tab:
don't forget set userinteractionenabled
yes
. left logical , direction verification you.
Comments
Post a Comment