2

我有UIScrollView很多UIImageViews。我需要将图像从另一个视图拖放UIScrollView到另一个视图中。在scrollView touch 之外工作。但是在滚动视图内部触摸不起作用。我使用touchesBegantouchesMoved等方法。请帮我。

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

}
4

4 回答 4

5

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

的子视图UIScrollview永远不会touchesBegan直接调用方法。您需要使用子视图进行自定义以获取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;
    }

     }
于 2013-02-26T06:03:21.463 回答
3

将 Tap 手势识别器添加到滚动视图以启用滚动视图内的触摸:

UITapGestureRecognizer *singlTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(methodName:)];
singlTap.cancelsTouchesInView = NO; //Default value for cancelsTouchesInView is YES, which will prevent buttons to be clicked
[scrollViewName addGestureRecognizer:singlTap];

然后以指定的方法编写您的代码。

-(void)methodName:(UITapGestureRecognizer *)gesture
{
     //your code here
}
于 2013-02-26T06:00:52.917 回答
2

我不知道这是否对你有帮助。但是尝试将 的exclusiveTouch属性设置UIImageViewYES

于 2013-02-26T06:03:47.127 回答
0

您是否将滚动视图添加为 IBOutlet?如果它来自 xib 并且正如你所说的触摸在滚动之外而不是内部可用,我猜你可能不小心取消了禁止触摸事件的滚动视图的userInteractionEnabled。并且您已将 UIImageViews 作为子视图添加到 UIScrollView。对于 UIImageView,您必须首先将userInteractionEnabled设置为YES,然后在必要时添加任何手势以获取事件或使用touchesBegan、touchesMoved方法。除非您设置启用交互,否则您不会在 UIImageView 上收到触摸事件。如果父视图我的意思是 UIScrollView 的交互被禁用,你也不会接触 UIImageView。希望这可以帮助 :)

于 2013-02-26T06:15:18.240 回答