简体   繁体   中英

IOS, setting up Gesture Tap on UILabel thats inside a UIScrollView

I have a UIScrollView and inside this Scroll View i have some controls. Specifically a label thats underlined and i have setup Tap Gesture for the UILabel but doesn't seem to fire the methods that the Tap Gesture is bound to.

Here is the code:

-(void)setupUserNameControls:(UIScrollView*)scroll{

            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 23, 70, 16)];
            label.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
            label.text = @"First Name";
            label.font = [UIFont fontWithName:@"Helvetica" size:14];
            [scroll addSubview:label];

            UIView *textView = [[UIView alloc] initWithFrame:CGRectMake(88, 16, 215, 28)];
            textView.backgroundColor = [UIColor colorWithRed:60.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
            [scroll addSubview:textView];

            UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(15, 55, 70, 16)];
            label2.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
            label2.text = @"Last Name";
            label2.font = [UIFont fontWithName:@"Helvetica" size:14];
            [scroll addSubview:label2];

            UIView *textView2 = [[UIView alloc] initWithFrame:CGRectMake(88, 48, 215, 28)];
            textView2.backgroundColor = [UIColor colorWithRed:60.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
            [scroll addSubview:textView2];

            UIImageView *imageViewSep = [[UIImageView alloc] initWithFrame:CGRectMake(10, 92, 300, 2)];
            UIImage *imageSep = [UIImage imageNamed:@"seperator.png"];
            imageViewSep.image = imageSep;
            [scroll addSubview:imageViewSep];


            UIView *viewImage = [[UIView alloc] initWithFrame:CGRectMake(10, 108, 58, 58)];
            viewImage.backgroundColor = [UIColor colorWithRed:132.0f/255 green:132.0f/255 blue:132.0f/255 alpha:1.0f];
            [scroll addSubview:viewImage];

            UIImageView *imageViewUser = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 48, 48)];
            UIImage *imageUser = [UIImage imageNamed:@"defaultuser.jpg"];
            imageViewUser.image = imageUser;
            [viewImage addSubview:imageViewUser];

            NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Click to select your user image"];
            [attributeString addAttribute:NSUnderlineStyleAttributeName
                        value:[NSNumber numberWithInt:1]
                        range:(NSRange){0,[attributeString length]}];

            UILabel *labelPickImage = [[UILabel alloc] initWithFrame:CGRectMake(88, 126, 215, 16)];
            labelPickImage.numberOfLines = 0;
            labelPickImage.textColor = [UIColor colorWithRed:59.0f/255 green:59.0f/255 blue:59.0f/255 alpha:1.0f];
            labelPickImage.attributedText = attributeString;
            labelPickImage.font = [UIFont fontWithName:@"Helvetica" size:14];
            [scroll addSubview:labelPickImage];


            UITapGestureRecognizer *userImageTextTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userImageSelectTap:)];
            [userImageTextTap setNumberOfTouchesRequired:1];
            [userImageTextTap setNumberOfTapsRequired:1];
            [labelPickImage addGestureRecognizer:userImageTextTap];

        }

        -(void)userImageSelectTap:(UIGestureRecognizer *)sender{

            picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            {
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            }else{
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            }
            [self presentModalViewController:picker animated:YES];
        }

        - (void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker {
            [[picker parentViewController] dismissModalViewControllerAnimated:YES];
        }

This is how i set up the UIScrollView and pass it in:

UIScrollView *userInfoScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 108, screenRect.size.width, screenRect.size.height - 40 - viewTop.bounds.size.height - viewTopBorder.bounds.size.height)];
[userInfoScrollView setBackgroundColor:[UIColor colorWithRed:228.0f/255 green:228.0f/255 blue:228.0f/255 alpha:1.0f]];
userInfoScrollView.userInteractionEnabled = YES;
[self.viewUser addSubview:userInfoScrollView];

make sure the label stays in front of the scrollView.

[scroll bringSubviewToFront:labelPickImage]; 

Also,

labelPickImage.userInteractionEnabled = YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM