Is there an way to make an invisible UIButton that will still “be there” and catch touch events for my UIImageView?

后端 未结 11 2009
悲哀的现实
悲哀的现实 2020-12-25 11:41

I thought to be clever and just put an transparent UIButton over an UIImageView with the exact frame size, so that I can wire it up easily with any event I like, for example

11条回答
  •  無奈伤痛
    2020-12-25 12:37

    lazyImageView = [[UIImageView alloc] init];

                            button=[[UIButton alloc]init];
                            [UIButton buttonWithType:UIButtonTypeRoundedRect];
                            [button addTarget:self
                                       action:@selector(aMethodForVideo:)
                             forControlEvents:UIControlEventTouchUpInside];
                            [button setBackgroundImage:[UIImage    imageNamed:@"BackTransparent.png"] forState:UIControlStateHighlighted];
                            [button setTitle:@"" forState:UIControlStateNormal];
    
                                    lazyImageView.frame=CGRectMake(x, y, w, h);
                                    button.frame=CGRectMake(x, y, w, h);
    

    Set frame of button and Image both have same frame .I use this code and working fine.Also set button background image forState:UIControlStateHighlighted so when you click on that when you see the click effect.

提交回复
热议问题