UIScrollView未检测到UITouch事件

时间:2012-04-13 05:47:52

标签: iphone uiview uiscrollview touch

我有UIScrollView个子视图UIView,此处UIView也有类型UIImageView的子视图。

层次结构类似于ViewController -> UIScrollView -> UIView -> UIImageView

全部,UIScrollView, UIView, UIImageView动态添加,全部启用userInteractionEnabled=YES,当用户滚动下一个SIX UIScrollView时,UIViews上可见的SIX视图将可见。

我正在实施UITouch事件以检测UIImageView

上的任何触摸
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //I want to detect particular UIImageView
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Here, user can move any UIImageView
}

但是,我无法检测到它的触摸。我试着用其他方式说here。还尝试了我的一些想法,但我找到了&意识到UIScrollView下的任何子视图都无法检测到任何对象。

我还尝试了另外一件事,我在UIView中添加了self.view作为子视图,任何有趣的知道,它会检测到touch !!

是否有人遇到过此类问题,有任何解决方案吗?任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

试试这个: -

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
 UITouch *touch = [[event allTouches] anyObject];
 //sampleImageView is the imageView.
 CGPoint location = [touch locationInView:sampleImageView];
 if ([touch view] == sampleImageView) 
  {
    //.....    
  }
}

用于触摸检测。检查它希望它有所帮助。谢谢:)

答案 1 :(得分:0)

你可以做一件事,

我已经面临同样的问题,所以 在UIscrollview中添加Caustom UIButton并设置图像并将所有按钮的标签定义到UIButton中。 这种方式非常顺利。

UIScrollView* scrForSubItem = [[UIScrollView alloc] initWithFrame:CGRectMake(10,2,1024,580)]; 
[scrForSubItem setAlwaysBounceVertical:YES];
scrForSubItem.minimumZoomScale = 1;
scrForSubItem.maximumZoomScale = 3;
scrForSubItem.scrollEnabled = YES;
scrForSubItem.pagingEnabled = NO;
scrForSubItem.directionalLockEnabled = YES;
scrForSubItem.showsVerticalScrollIndicator = YES;
scrForSubItem.backgroundColor = [UIColor clearColor];
scrForSubItem.delegate = self;

int xOffset = 0;
int yOffset = 0;
for (int i = 0; i < [currnetitem count]; i++) {


UIImageView* imgViewBtn=[[[UIImageView alloc]initWithFrame:CGRectMake(xOffset,yOffset,            246,170)] autorelease];

    [imgViewBtn setUserInteractionEnabled:YES];
    [imgViewBtn setContentMode:UIViewContentModeScaleAspectFit];
    imgViewBtn.image=btnImg;

    UIButton *btnSelectTile = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnSelectTile setFrame:CGRectMake(0,0,246,170)];
    [btnSelectTile setTag:i];
    [btnSelectTile setBackgroundColor:[UIColor clearColor]];
    [btnSelectTile addTarget:self action:@selector(scrImagePressed:) forControlEvents:UIControlEventTouchUpInside];
    [imgViewBtn addSubview:btnSelectTile];

    [self.scrForSubItem addSubview:imgViewBtn];
   xOffset += btnSelectTile.frame.size.width+10;

    if(xOffset>scrollWidth)
    {
        xOffset=0;
        yOffset += btnSelectTile.frame.size.height+10;
        [scrForSubItem setContentSize:CGSizeMake(xOffset,yOffset)];
    }
    else{

        [scrForSubItem setContentSize:CGSizeMake(xOffset,170)];

    }

    [self.viewa ddSubview:scrForSubItem]; 

}

答案 2 :(得分:0)

//创建.h文件

@interface AppScrollView:UIScrollView {

}

  • (ID)initWithFrame:方法(的CGRect)帧;

@end

//创建.m文件

导入“AppScrollView.h”

@implementation AppScrollView

  • (ID)initWithFrame:方法(的CGRect)帧 { if([super initWithFrame:frame]){ } 回归自我; }

    • (void)touchesEnded:(NSSet *)触及withEvent:(UIEvent *)事件 {

    //如果不拖动,请将事件发送给下一个响应者

    if(!self.dragging)

    [self.nextResponder touchesEnded: touches withEvent:event]; 
    

    否则

    [super touchesEnded: touches withEvent: event];
    

    }

    @end

现在在你的UIViewController

//为滚动视图创建对象

AppScrollView * scrollView;