UIImageView from array and touch? for IPhone
i have this method Code:
- (void)addImageSubViewAtX:(CGFloat)x atY:(CGFloat)y {
CGRect myImageRect1 = CGRectMake(x, y, 30.0f, 30.0f);
myImage1 = [[UIImageView alloc] initWithFrame:myImageRect1];
[myImage1 setImage:[UIImage imageNamed:@"status_finish.gif"]];
[self.view addSubview:myImage1];
}
now i am using this to call images Code:
[self addImageSubViewAtX:160.0 atY:190.0];
and
[self addImageSubViewAtX:10.0 atY:190.0];
but touch method is working only on 1 image not both
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self.view];
if (CGRectContainsPoint(CGRectMake(myImage1.frame.origin.x, myImage1.frame.origin.y, myImage1.frame.size.width, myImage1.frame.size.height ), p))
{
[pieMenu showInView:self.view atPoint:p];
}
}
how to 开发者_开发知识库make this touch working for both of them
Okay,
you are calling 2 times the - (void)addImageSubViewAtX:(CGFloat)x atY:(CGFloat)y
- Method.
In this method you're setting the pointer of myImage1 to a new instance of a UIImageView. So myImage1 is the reference to the last UIImageView you have added.
Thats why it just works for one imageView.
You are reinitializing the the same image view (myImage1) in your addImageSubViewAtX method not creating and adding a new one. So only the latest image view is accessed in the touch method when you use myImage1.
Instead add a new image view every time. Give it a particular tag before adding and check for a touch on the view with that tag.
Something like :
- (void)addImageSubViewAtX:(CGFloat)x atY:(CGFloat)y {
CGRect myImageRect1 = CGRectMake(x, y, 30.0f, 30.0f);
UIImageView myImage1 = [[UIImageView alloc] initWithFrame:myImageRect1];
[myImage1 setImage:[UIImage imageNamed:@"status_finish.gif"]];
myImage1.tag = 1000;
[self.view addSubview:myImage1];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([[touch view] tag] == 1000)
{
[pieMenu showInView:self.view atPoint:p];
}
}
精彩评论