如何使 UIImageView 成为按钮?

Posted

技术标签:

【中文标题】如何使 UIImageView 成为按钮?【英文标题】:How to make a UIImageView a button? 【发布时间】:2014-06-19 06:26:49 【问题描述】:

我有一个这样的 UIImageView:

UIImage *image = [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;

[self.view addSubview:imageView];

几个问题/问题:

    如何将此按钮设为可点击按钮?

    我想链接到设置(在我的应用程序中),但我是否必须像许多应用程序使用的设置图标一样创建该图像轮,或者它是 ios 7 中的标准图标? 这就是我说的图标:

    为什么我的边框不显示?

【问题讨论】:

你可以在你的 imageview 上放置一个自定义类型的 UIButton 为什么不尝试添加一个 UIButton,然后在该按钮上添加背景图像? 【参考方案1】:

1)如果你想要一个按钮,你应该使用UIButton和[UIButton setImage: forState:]

2) 或者,您可以将UITapGestureRecognizer 添加到您当前的UIImageView

【讨论】:

是的,我将切换到 UIButton【参考方案2】:

这里有两个选择你必须使用你的方法

.h 文件

 #import <QuartzCore/QuartzCore.h>  //for radius and corner



1. UIButton


   UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
 [btn1 setTitle:@"Cool title" forState:UIControlStateNormal];
[btn1 setFrame:CGRectMake(7, 7, 150, 160)];
[btn1 setImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(selectFav) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];


-(void) selectFav

  //what ever you do like


 

2. UITap Gesture for UIImage View


 UIImage *image = [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

  imageView.frame = CGRectMake(0, 0, 30, 30);
  imageView.layer.cornerRadius = 5.0;
  imageView.layer.masksToBounds = YES;
 imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;


   UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:)];

 tapGesture1.numberOfTapsRequired = 1;

[tapGesture1 setDelegate:self];

 [imgView addGestureRecognizer:tapGesture1];

[self.view addSubview:imageView];

- (void) tapGesture: (id)sender

 //handle Tap...

【讨论】:

在我的选择中主要用于你的图像的取消按钮,它很好用【参考方案3】:

试试:

UIImage *image = [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;

/* Here's the button Start */
UIButton *theButton = [[UIButton alloc]initWithFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height)];
theButton.backgroundColor = [UIColor clearColor];
theButton.showsTouchWhenHighlighted = YES;
[theButton addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside];

[imageView addSubview:theButton];
/* Here's the button End */

[self.view addSubview:imageView];

你的边界:

0.1 means is 1/10th of a point - try 1.0 for a nice fine line

同时设置:

imageView.clipsToBounds = YES;

【讨论】:

【参考方案4】:

1:如果你有图像视图并且想要点击

UIImage *image = [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

2:像这样向图像视图添加手势,但不要忘记 setUserInteractionEnabled:YES

UITapGestureRecognizer *TapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(markerMove:)];

    [TapGestureRecognizer setNumberOfTapsRequired:1];
    [TapGestureRecognizer setNumberOfTouchesRequired:1];
    [imageView setUserInteractionEnabled:YES];
    [imageView addGestureRecognizer: TapGestureRecognizer];

【讨论】:

以上是关于如何使 UIImageView 成为按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何在圈子 Swift 5 中设置 UIImageView

代码中的 UIImageView 和自动布局

如何将图片分配给 UIImageView (Swift 3)

如何在swift中使用带有completionHandler的UIImageView load()函数

如何以全分辨率从 UIImageView 获取旋转、缩放和平移的图像?

使 UIImageView 模糊