0

I've done some research and haven't been able to find an answer to my question yet....

I currently have a View in my app that only has Labels, for example:

"Total Area: 100

-----Material A Area: 75

-----Material B Area: 25

Total....."

What I want to do with this is allow the user to interact with the "Total Area" Label. So when the user touches the label, the "Material" labels will hide themselves and the text below that will slide up, like:

"Total Area: 100

Total....."

And, of course, when the user touches the "Total Area" label again, the "material" labels will reappear.

I know that in Xcode there is a check box that you can check to Enable User Interaction, but I'm pretty lost on where to go from here. I'm guessing that I need to create my own class that will inherit from UILabel to handle the user input...? And I'm not sure about how to get the "sliding/hiding" to work. Any help would be appreciated. Thanks.

erics
  • 111
  • 11
  • May be u can show label with this effect it will give good animation effect too. https://github.com/cbpowell/MarqueeLabel – Wolverine Dec 15 '15 at 10:48

1 Answers1

0

There are a few ways you could do this. One way would be to detect a tap event using the following code:

self.label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelMethod)];
[self.label addGestureRecognizer:tapGesture];

Then in labelMethod you could set the hidden property of your other labels to YES and use the UIView animateWithDuration block method to translate your label up (edit the top space constraint and commit your animations).

Alex Wulff
  • 2,039
  • 3
  • 18
  • 29
  • Thanks! :) I'll give this a try! Do you know what parameters/return value the "labelMethod" will need, or can you provide a link to the documentation? – erics Dec 14 '15 at 23:19
  • labelMethod is just a generic name that I made up. You can replace that with the name of any method you want, just declare it with -(void)yourMethod or something of that nature. Good luck! – Alex Wulff Dec 14 '15 at 23:21
  • There's a post with a similar answer as this. This may give more information if you need it. http://stackoverflow.com/questions/4743457/how-to-customize-uilabel-clickable – Gabriel Pires Dec 15 '15 at 00:16
  • This worked like a charm! Thank you very much :) I do have one question though: if I have more than one label to animate, do I need to animate them all individually, or is there a shortcut I can take to animate one label and all the other labels will follow because of their constraints to the animated label? I tried to add a vertical spacing constraint, but it didn't follow the animated label.... – erics Dec 15 '15 at 17:06
  • You could try autolayout with constraints, however I find that it's generally more trouble than it's worth. I would recommend making a method that accepts a UILabel as a parameter -(void)someMethod: (UILabel *)toAnimate and then animates the label however you want. Then, you could only have one line of code to animate each label wherever you're doing it. [self someMethod:myLabel] – Alex Wulff Dec 15 '15 at 20:16