0

我正在使用一个 UIScrollView,其中有一些 UIButtons,为了识别我按下了上面的按钮,我创建了一个 UIScrollView 的子类,在其中重写了方法

- (BOOL) touchesShouldCancelInContentView (UIView *) view

但只有当触摸是一个旅程时才会进入那个方法,当我按下屏幕时永远不会出现更多。

我这样创建的 UIScrollView:

`

CGSize containerSize = CGSizeMake(320.0f, 480.0f);
CGSize containerSize2 = CGSizeMake(640.0f, 960.0f);
self.scrollView = [[UIScrollViewWithButtons alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize}];

[self.scrollView setMinimumZoomScale:0.5f];
[self.scrollView setMaximumZoomScale:1.0f];
[self.scrollView setZoomScale:0.5f];
[self.scrollView setDelegate:self];

self.scrollView.bounces = NO;
self.scrollView.bouncesZoom = NO;
self.scrollView.delaysContentTouches = NO;

self.scrollView.userInteractionEnabled = YES;

[self.scrollView setContentInset:UIEdgeInsetsZero];

[self.view addSubview:self.scrollView];

self.containerView = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize2}];
self.containerView.userInteractionEnabled = YES;
[self.scrollView addSubview:self.containerView];

[self.scrollView setClipsToBounds:NO];

//instanciar uimageview
fondo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 640, 960)];
[self.containerView addSubview:fondo];
[fondo setImage:[UIImage imageNamed:@"Grande.png"]];

self.scrollView.contentSize = containerSize2;

和按钮添加

imagen = [UIButton buttonWithType:UIButtonTypeCustom];
        [imagen addTarget:self action:@selector(pulsadoIzq) forControlEvents:UIControlEventTouchUpInside]; 

        [imagen setTitle:@"" forState:UIControlStateNormal];
        [imagen setFrame:CGRectMake(xUser, yUser, 50.0f, 50.0f)];
        [imagen setImage:[UIImage imageNamed:@"boton.png"] forState:UIControlStateNormal];
[self.containerView addSubview:fichaUserIzq];

        imagen.center = CGPointMake(xUser, yUser);`enter code here`

我需要捕捉屏幕上的点击到按钮 我该怎么办?

我也想知道,因为如果我实现了这段代码:

img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img.png"]];
UIPanGestureRecognizer *panRecg = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(imgDragged:)];

img.userInteractionEnabled = YES;
[img addGestureRecognizer:panRecg];
img.center = CGPointMake(160, 436);

[self.scrollView addSubview:img];

当我单击图像时没有任何反应,没有进入方法,只有当我按下并拖动时才会出现。我也想在按下这个图像时捕捉,所以我只在移动时捕捉。

4

1 回答 1

0

我解决了这个问题,我实现了这个方法:

-(void)singleTapGestureCaptured:(UITapGestureRecognizer*)gesture{
CGPoint touchPoint = [gesture locationInView:self.scrollView];
if ((touchPoint.x > 88)&&(touchPoint.x < 226)&&(touchPoint.y > 172)&&(touchPoint.y < 319)) {
    if (rodando) {
        [self pararAnimacionDado];
    }
}

}

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; [scrollView addGestureRecognizer:singleTap];

于 2013-07-26T14:33:00.920 回答