如何使UIImageView成为一个按钮?

时间:2014-06-19 06:26:50

标签: ios objective-c uiimageview uibutton

我有一个像这样的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];

一些问题/问题:

  1. 如何将其设为可点击按钮?
  2. 我想链接到设置(在我的应用程序中),但我是否必须像许多应用程序使用的设置图标一样创建该图像轮,或者它是iOS 7中的标准图标? 这是我正在谈论的图标:iOS Settings Icon

  3. 为什么我的边框不显示?

4 个答案:

答案 0 :(得分:2)

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

2)或者,您可以在当前UIImageView

中添加UITapGestureRecognizer

答案 1 :(得分:0)

这里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...
}

答案 2 :(得分:0)

尝试:

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;

答案 3 :(得分:0)

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];