iphone - 检测添加为 UIScrollView subview 的 UIImage 触摸

标签 iphone ios uiscrollview uitouch

我有 UIScrollView 和许多 UIImageViews。我需要将图像从 UIScrollView 拖放到另一个 View 。在 scrollView 之外触摸是有效的。但是在 ScrollView 中触摸不起作用。我使用了 touchesBegan,touchesMoved 等方法。请帮我。

-(IBAction)selectBut:(id)sender
{

 scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(x,y,w,h)];

 scrollView.userInteractionEnabled = YES;

 int y = 0;

 for (int i = 0; i < [myArray count]; i++) {

UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, y, 75, 30)];

 image.userInteractionEnabled = YES;

  y=y+35;

 [scrollView addSubview:image];

}

[self.view addSubview:scrollView];

[scrollView setContentSize:CGSizeMake(150, 300)]

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

   UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 1) {

        NSLog(@"One touch !!");
    }

}

最佳答案

您需要使用您自己的继承 UIImageView 的 View 自定义UIImageView。在该自定义子类中提供触摸方法并将其添加到您的 UIScrollView..

UIScrollview 的 subview 永远不会直接调用touchesBegan 方法。您需要使用 subview 进行自定义以获取添加的 subview /自定义 View 的 touchesBegan 属性。

我的意思是说采用 ImageView 的子类

CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[imageView setUserInteractionEnabled:YES];
[scrollView addSubview:imageView];
[imageView release];

CustomImageView 类应该像 UIImageView 一样继承

 @interface CustomImageView : UIImageView
  {
  }

在#.m 文件中

  #import "CustomImageView.h"

@implementation CustomImageView


    - (id)initWithFrame:(CGRect)frame
   {
self = [super initWithFrame:frame];
if (self) {

}
return self;
}



    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      NSLog(@"%s", __FUNCTION__);

     }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {


    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {


    UITouch *touch = [touches anyObject];

      if ([touch tapCount] == 2) {
    drawImageView.image = nil;
    return;
    }

     }

关于iphone - 检测添加为 UIScrollView subview 的 UIImage 触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15082309/

相关文章:

ios - : This application is modifying the autolayout engine from a background thread如何解决

iphone - iOS 分页控件(导航)

iphone - 在 xcode 应用程序中的何处存储基本 url 字符串?

ios - 为什么我的 sender.tag 在 SWIFT 中返回为 0

iphone - 如何在应用启动前获取主屏幕截图

ios - 自定义 UICollectionView 分页过渡

iphone - 使用 iPhone 上的菜单复制 Android ViewPager 类型功能

android - iPhone 有 9-patch 加载器吗?

ios - glDrawElements显示问题

iphone - KKGridView 与 AQGridView