[英]iOS touching a UIImageView
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
container = [[UIView alloc] initWithFrame:self.view.frame];
UIImageView* main_im0 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon1.png"]];
[container addSubview:main_im0];
[self.view addSubview:container];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *tt = [touches anyObject];
UIImageView *touchedview=(UIImageView*)[tt view];
NSArray *views = [container subviews];
for (UIImageView* im in views)
{
UIImageView* focus=im;
if (touchedview==focus)
{
}
}
}
我有一段代码来设置一个名为main_im0的ImageView,将其放入UIView容器中,然后将其放入视图中。 当我按一下main_im0时,我期望触摸功能会达到touchedview == focus的条件。 但是,我无法激活该条件? 怎么了?
请启用
main_im0.userInteractionEnabled = YES;
默认情况下, UIImageView
为No
杰森(Jason),也许我在误解您的代码,但是您是否不打算在UIView上实现点击手势呢?
您可以使用:
// enable user interaction with this view
main_img0.userInteractionEnabled = YES;
// setup a tap gesture to call a method once triggered (like a javascript mouseClick event)
UITapGesture *tapGesture = [[UITapGesture alloc] initWithTarget:self selector:@selector(myMethodToDoSomething)];
[main_img0 addGestureRecognizer:tapGesture];
// if you are not using Automatic Reference Counting, then remember to release your allocated tapGesture object
[tapGesture release];
...somewhere later in your code....
// method to do something
-(void)myMethodToDoSomething
{
NSLog(@"This method executed");
}
现在,当您点击main_img0视图时,将执行方法“ myMethodToDoSomething”
顺便说一句,如果您只想使用自己的Photoshop设计的图像来创建自定义外观的按钮,则只需使用代码创建UIButton并设置UIButton的背景图像属性即可。 像这样:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
[button setImage:[UIImage imageNamed:@"mybutton.png"] forControlState:UIControlStateNormal];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.