mirceapasoi
mirceapasoi

Reputation: 325

How to change the style of an UIView when it's tapped?

I'm making different UIView's tappable (they're not inheriting from UIControl) using the following code:

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTappedOnLink:)];
[labelView setUserInteractionEnabled:YES];
[labelView addGestureRecognizer:gesture];

but I'd also like to change the style when they're highlighted. How do I do that?

Upvotes: 4

Views: 5101

Answers (4)

user754270
user754270

Reputation: 1

well, I have not tested it, just a suggestion, please handle touchesbegin for this view,and call [labelView addGestureRecognizer:gesture]; function. maybe you should use another function, - (void)removeGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer .

Upvotes: 0

mirceapasoi
mirceapasoi

Reputation: 325

I managed to solve this by adding an UIControl as a subview in the UIView. The UIControl is the same size has a transparent background that changes when it's highlighted. Works like a charm!

Upvotes: 1

TheBlack
TheBlack

Reputation: 1245

Attach UILongPressGestureRecognizer instead of UITapGestureRecognizer to parent view and set it's properties to your liking. The way to track and respond to selection is to implement userTappedOnLink method in appropriate way. This method will be called lots of times in short amount of time when gesture recognizer is activated and you know what's happening by tracking recognizer states.

Implement UIView subclass and create methods, like select and deselect, and customize view properties for each. Then it's only matter of finding which UIView subclass to select or deselect and that's easily done with UIGestureRecognizer method returning point in parent view and iterating trough it's subviews while checking if touch point is inside of particular subview frame.

- (IBAction)userTappedOnLink:(UIGestureRecognizer*)sender
{

    switch (sender.state) 
    {
        case UIGestureRecognizerStateBegan:
{

        CGPoint touchPoint = [sender locationInView:self.parentView];
        for (UIView *subView in [self.parentView subViews) 
        {
            if (CGRectContainsPoint(subView.frame, tapPoint))
            {
                self.activeSubView = self.subview;
                            break;
            }
        }
                [self.activeSubView select];

        case UIGestureRecognizerStateChanged:[self.activeSubView doNothing];; break;
        case UIGestureRecognizerStateEnded:[self.activeSubView deSelect]; self.activeSubView = nil; break;
    }
}

Upvotes: 3

danny_23
danny_23

Reputation: 503

There are two ways of handling events in iOS. The first one is to use UIView subclassing and override the methods that are inherited by UIView from UIResponder class (i.e. touchesBegan:withEvent, touchesMoved:withEvent, touchesEnded:withEvent, touchesCancelled:withEvent).

The second way is to create new instance of gesture recognizer class and add it to your UIView object (just like you did) and then create a handler.

- (IBAction)userTappedOnLink:(UIGestureRecognizer *)sender {
    // Changing view properties.
}

In both cases you can change the UIView properties. You can find some useful information in "Event handling guide" by Apple. There is a lot of reading but you can have a look only at the "related sample code" (Touches and SimpleGestureRecognizers).

The way you change the style properties of the interface elements in your application depends on what those properties are. Sometimes they can be animated sometimes they're not. Usually the code that changes view properties is placed inside the touchesBegin function or gesture recognizer handler. MoveMe sample code shows how to change views properties and animate them. In the Gesture recognizers chapter of the "Event handling guide" frame properties are changed.

Upvotes: 1

Related Questions