UIImageView as button

后端 未结 5 1129
庸人自扰
庸人自扰 2020-12-12 17:48

I\'m trying to make a UIImageView accept actions and fire them everytime it clicked on the UIImageVie, but i\'m having hard time in getting it to work, please help me out

相关标签:
5条回答
  • 2020-12-12 18:07

    All you have to do is drag a Tap Gesture Recognizer onto the imageView (in Storyboard) and enable User Interaction on the Attributes Inspector. If you want to use the image as a link to another view controller, just control-drag the Tap Gesture Recognizer icon in the bottom of the view controller to the destination view controller.

    0 讨论(0)
  • 2020-12-12 18:08

    UIImageView is not a control, you can't add a target for control events to it. You have several options:

    Place an invisible UIButton over the UIImageView and add your target to that. You can set the button's style to custom and its text (and image) to nothing to make it invisible.

    Use just a UIButton. You can add a background image to it.

    Add a UITapGestureRecognizer to your image view and implement it's touch handler method.

    Subclass UIImageView and override the touch handling methods to do what ever you need to do.

    0 讨论(0)
  • 2020-12-12 18:09

    i am not a pro but you can use button for this

    UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    imageButton.frame = CGRectMake(0, 0, 200, 200);
    [imageButton setImage:[UIImage imageNamed:@"Your_Image.png"] forState:UIControlStateNormal];
    imageButton.adjustsImageWhenHighlighted = NO;
    [imageButton addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:imageButton];
    
    0 讨论(0)
  • 2020-12-12 18:11

    Objective-C

    In your viewDidLoad: method write this:

    -(void)viewDidLoad {
    
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
        [imageview setImage:[UIImage imageNamed:@"image.png"]];
        [imageview setUserInteractionEnabled:YES];
    
        UITapGestureRecognizer *singleTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapping:)];
        [singleTap setNumberOfTapsRequired:1];
        [imageview addGestureRecognizer:singleTap];
    
        [self.view addSubview:imageview];
    }
    

    Then call your gesture method like this:

    -(void)singleTapping:(UIGestureRecognizer *)recognizer {
        NSLog(@"image clicked");
    }
    

    Swift

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let imageView: UIImageView = UIImageView(frame: CGRect(x: 100.0, y: 100.0, width: 100.0, height: 100.0))
        imageView.image = UIImage(named: "image.png")
        imageView.isUserInteractionEnabled = true
    
        let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.singleTapping(recognizer:)))
        singleTap.numberOfTapsRequired = 1
        imageView.addGestureRecognizer(singleTap)
    
        self.view.addSubview(imageView)
    }
    
    @objc func singleTapping(recognizer: UIGestureRecognizer) {
        print("image clicked")
    }
    
    0 讨论(0)
  • 2020-12-12 18:20

    UIImageView is not a UIControl so it doesn't have the addTarget:action:forControlEvents method as part of its interface. You can use a gesture recognizer instead.

    0 讨论(0)
提交回复
热议问题