0

我有一个UIScrollView带有子视图的UIView,这里UIView也有类型的子视图UIImageView

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

全部,UIScrollView, UIView, UIImageView动态添加,全部启用userInteractionEnabled=YESUIScrollView当用户滚动时,会立即看到六个视图,下一个六个视图UIViews将是可见的。

我正在实现 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
}

但是,我无法检测到触摸它。我尝试这里说的其他一些方法。也尝试了一些我的想法建议我,但我发现并意识到下面的任何子视图UIScrollView都无法检测到任何对象。

我还尝试了另一件事,我在其中添加了一个UIView子视图self.view,任何有趣的事情都可以检测到touch

有没有人遇到这种问题,有什么解决办法吗?任何帮助,将不胜感激。

4

3 回答 3

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) 
  {
    //.....    
  }
}

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

于 2012-04-13T06:11:15.330 回答
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]; 

}

于 2012-04-13T06:24:42.953 回答
0

// 创建.h文件

@interface AppScrollView : UIScrollView {

}

  • (id)initWithFrame:(CGRect)frame;

@结尾

// 创建 .m 文件

导入“AppScrollView.h”

@implementation AppScrollView

  • (id)initWithFrame:(CGRect)frame { if ([super initWithFrame:frame]){ } return self; }

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

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

    if (!self.dragging)

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

    别的

    [super touchesEnded: touches withEvent: event];
    

    }

    @结尾

现在在你的 UIViewController

// 为滚动视图创建对象

AppScrollView* 滚动视图;

于 2012-04-13T06:25:14.350 回答