TouchesMoved 与 UIButton xcode 4.4 iOS 5.1.1

2024-01-29

我知道这个问题似乎已经被提出和回答,但我无法解决我的问题。

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

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

当触摸开始于视图背景时它工作正常,但当触摸开始于 uibuttons 时则不起作用。

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

今天我刚刚安装了 OS X Mountain 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,但主要的 TouchMoved 仅执行一次。 (self.nextResponder 似乎只工作一次)

是否发生了某些变化导致它无法在 xcode 4.4 或 iOS 5.1.1 上运行?

当我第一次尝试这样做时(在 xcode 3.x 和 iOS 4.x 上),我在这里找到了解决方案:有没有办法在 iPhone 上传递触摸? https://stackoverflow.com/questions/631427/is-there-a-way-to-pass-touches-through-on-the-iphone

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

你可以帮帮我吗?

多谢。

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


我不久前已经解决了这个问题,但不记得如何解决,但我的文件存在差异,因此我将在这里写下有效的新版本。

//  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 中放置一个平移手势识别器,并将其链接到 file.m 中定义的操作 panRecognizer:

// 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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TouchesMoved 与 UIButton xcode 4.4 iOS 5.1.1 的相关文章

随机推荐