2013-02-26 115 views
2

我有UIScrollView与许多UIImageViews。我需要将图像从UIScrollView拖放到另一个视图。在scrollView触摸之外工作。但内部滚动视图触摸不起作用。我用touchesBegan,touchesMoved等方法。请帮帮我。检测触摸的UIImage作为UIScrollView子视图添加

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

} 
+0

你有没有尝试过将UIImageView设置为'[self setUserInteractionEnabled:TRUE]'?其默认情况下禁用,如果我没有错。 – 2013-02-26 05:58:19

+0

@IkmalEzzani,UserInteractionEnabled为true,但它不起作用。 – IKKA 2013-02-26 06:01:16

+0

UIScrollView touchesBegan内部不适用于子视图。只需验证我的答案。 – Madhu 2013-02-26 07:09:33

回答

4

你需要用自己的观点继承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#.m文件被继承像

@interface CustomImageView : UIImageView 
    { 
    } 

#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; 
    } 

    } 
+1

非常感谢你......你很棒.. – IKKA 2013-02-26 08:04:07

+0

欢迎你:) – Madhu 2013-02-26 08:07:32

+0

@madhu:我感谢你的回答。真的很棒,乐于助人! – 2016-12-03 06:22:44

3

添加点击手势识别滚动视图用于使滚动视图内的触摸:

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 
} 
+1

不要忘记将exclusiveTouch设置为yes,就像下面提到的Bhargavi一样......如果你不这样做,那么当用户只是滚动时,用户可能会意外地“点击”图像...'exclusiveTouch'将防止意外触摸如果用户只是滚动浏览(尤其是启用了Paging),他们不会意外触发50 gillion(实数)的抽头。 – 2013-02-26 06:06:20

1

我不不知道这是否有助于你。但尝试将UIImageViewexclusiveTouch属性设置为YES

+0

这很重要!许多人将这一切抛出。 – 2013-02-26 06:05:11

0

您是否将滚动视图添加为IBOutlet?如果它从xib和你所说的触摸在滚动外部可用而不在内部,我想你可能会意外地取消选中userInteractionEnabled来抑制触摸事件。并且您已将UIImageView添加为UIScrollView的子视图。对于UIImageView的,你必须先设置userInteractionEnabledYES,然后添加任何手势,如果有必要获取事件或使用的touchesBegan,touchesMoved方法。除非您设置互动功能,否则您不会在UIImageView上收到触摸事件。如果父视图的意思是UIScrollView的交互被禁用,你也不会触及UIImageView。希望这有助于:)

+0

我以编程方式添加了UIScrollView.I设置scrollView.userInteractionEnabled = YES。但触摸不工作。 – IKKA 2013-02-26 06:26:52

+0

你的意思是在图像上的触摸不工作? – Meera 2013-02-26 06:29:38

+0

雅,触摸图像不工作.UIScrollView包含所有图像。 – IKKA 2013-02-26 06:32:06