【问题标题】:Click Event on UIImageView programmatically以编程方式单击 UIImageView 上的事件
【发布时间】:2014-05-12 11:02:13
【问题描述】:

我正在显示来自代码的图像集合, 这是我的代码:

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

我想以编程方式处理addimageView 上的触摸事件。

【问题讨论】:

    标签: objective-c ios7 uiimageview


    【解决方案1】:

    我会建议不同的方法。将点击手势识别器添加到图像视图。这可能是最简单的解决方案。如果你有 addImageView 方法来添加图像视图,那么继续这样,

    - (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);
    }
    

    【讨论】:

      【解决方案2】:

      您必须继承它并覆盖-touchesBegan: 方法,或者您可以将手势识别器添加到此对象。这是有关此的文档: Event Handling Guide for iOS

      好的方法是继承这个类并在类中添加手势识别器。

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 2022-06-12
        • 2013-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多