2

我知道这个问题似乎已经被问及并得到了回答,但我无法解决我的问题。

我想移动视图中的对象以跟随手指的水平滑动(转到上一页或下一页)

为此,我使用了 TouchesBegan、TouchesMoved 和 TouchesEnded 方法。

当在视图背景上开始触摸时它工作正常,但在 uibutton 上开始触摸时不起作用。

为了解决这个问题,我对我想要允许手势的 uibuttons 进行了子类化,当我在 xCode 3.x 和 iOS 4.x 上时它工作正常

今天我刚刚安装了 OSX Moutain Lion 并开始在 xCode 4.4 和 iOS 5.1.1 (iPAD) 上工作。在这里,我使用的是同样运行良好但不再工作的应用程序。

这是我的 UIButton 子类:

//  myUIButton.h

#import <UIKit/UIKit.h>

@interface myUIButton : UIButton

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

@end


//  myUIButton.m

#import "myUIButton.h"

@implementation myUIButton

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"uibutton touches began");
    [self.nextResponder touchesBegan:touches withEvent:event];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"uibutton touches moved");
    [self.nextResponder touchesMoved:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"uibutton touches ended");
    [self.nextResponder touchesEnded:touches withEvent:event];
}

@end

以下是我的主类中的方法:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"view touches began");
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"view touches moved");
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"view touches ended");
}

当我在 UIButton 上开始触摸时,我从子类中获取每个 NSLog,但主要的 touchesMoved 只执行一次。(self.nextResponder 似乎只工作一次)

有什么改变使它不能在 xcode 4.4 或 iOS 5.1.1 上运行吗?

当我第一次尝试这样做时(在 xcode 3.x 和 iOS 4.x 上),我在这里找到了解决方案: 有没有办法在 iPhone 上传递触摸?

但它不再起作用了......

你可以帮帮我吗?

非常感谢。

编辑:使用 [super touches...] 代替或更多 [self.nextResponder touches...] 的问题是相同的

4

1 回答 1

0

我刚刚解决了这个问题,不记得如何解决了,但是我的文件存在差异,所以我会在这里写下有效的新版本。

//  myUIButton.h
#import <Foundation/Foundation.h>

@interface myUIButton : UIButton {
}

@end

.

//  myUIButton.m

#import "myUIButton.h"

@implementation myUIButton

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"uibutton touches began");
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"uibutton touches moved");
    [super touchesMoved:touches withEvent:event];
    [self.nextResponder touchesMoved:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"uibutton touches ended");
    [super touchesEnded:touches withEvent:event];
    [self.nextResponder touchesEnded:touches withEvent:event];
}

@end

在我需要这种行为的每个视图中,我在 file.xib 中放置一个 Pan Gesture Recognizer,我链接到动作 panRecognizer: 在 file.m 中定义

// file.m

@synthesize slide_origin_x;

-(IBAction)panRecognizer:(UIPanGestureRecognizer *)recognizer {
    // Recording movement
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        self.slide_origin_x = [NSNumber numberWithFloat:recognizer.view.center.x];
    }

    // Choose an action
    else if (recognizer.state == UIGestureRecognizerStateEnded) {
        int to_launch_action = 100; // Action choosen after 100 pixels
        int touch_move = [self.slide_origin_x intValue] - recognizer.view.center.x;
        if (touch_move > (0 + to_launch_action)) {
            /*
             * What happens if the view slide to the left (eg : go_to_next)
             */
        } else if (touch_move < (0 - to_launch_action)) {
            /*
             * What happens if the view slide to the right (eg : go_to_previous)
             */
        }
        // The view goes back to normal
        recognizer.view.center = CGPointMake([self.slide_origin_x intValue], recognizer.view.center.y);
    }

    // The view is moved
    else {
        CGPoint translation = [recognizer translationInView:self.view];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                             recognizer.view.center.y);
        [recognizer setTranslation:CGPointMake(0,0) inView:self.view];
    }
}

仅供参考:这也适用于 XCode5 和 SDK6.1

于 2014-01-16T15:15:55.973 回答