문제

I'am displaying a collection of images from code, This is my code:

UIImageView *addview;
myimgeview *addimageview =[[myimgeview alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d",PhotoName,i]] text:[textArray objectAtIndex:i]];

I want to handle the touch event on the addimageView programmatically.

도움이 되었습니까?

해결책

I would suggest different approach. Add tap gesture recognizer to the imageview. That is probably easiest solutions. If you have addImageView method that adds image view to view then, go on like this,

- (void)addImageView
{
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,100,100);
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self selector:@selector(tapped:)];
    [imageView addGestureRecognizer:tap];
}

- (void)tapped:(UITapGestureRecognizer*)tap
{
    NSLog(@"%@", tap.view);
}

다른 팁

You have to subclass it and override -touchesBegan: method or you can add gesture recognizer to this object. Here is documentation about that: Event Handling Guide for iOS

Good way is to subclass this class and add gesture recognizer inside the class.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top