2012-08-16 104 views
0
- (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) 
    { 

    } 

} 

}iOS的触摸一个UIImageView

我有这一段代码建立称为main_im0一个ImageView的其放入一个UIView容器,该容器再装进图。当我使用main_im0时,我预计触摸功能会触及touchview == focus的条件。但是,我无法启动该条件?怎么了?

回答

4

请启用

main_im0.userInteractionEnabled = YES; 

默认情况下它是NoUIImageView

1

杰森,也许我误解你的代码,但是你不打算一轮对实现上一个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];