本文介绍了使用UIButton xcode 4.4 iOS 5.1.1进行TouchesMoved的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I know this question seems to be already asked and answered but I can't resolve my issue.

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

I want to move the objects of my view to follow horizontal slides of a finger (going to previous or next page)

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

For this, I use TouchesBegan, TouchesMoved and TouchesEnded methods.

当在视图背景上开始触摸时,它可以正常工作,但在uibuttons上开始触摸时,则不起作用.

It works fine when the touches began on the view background but does not work when touches began on uibuttons.

为解决该问题,我将uibuttons子类化了,我希望它允许该手势,并且当我使用xCode 3.x和iOS 4.x时,它工作正常.

To solve the problem, I have subclassed the uibuttons which i want to allow the gesture and it was working fine when i was on xCode 3.x and iOS 4.x

今天,我刚刚安装了OSX Moutain Lion,并开始在xCode 4.4和iOS 5.1.1(iPAD)上工作.在这里,我使用的是同一个应用程序,该应用程序运行良好,无法正常工作.

Today I just installed OSX Moutain Lion and started to work on xCode 4.4 and iOS 5.1.1 (iPAD).And here I am with the very same app that worked fine and not working anymore.

这是我的UIButton子类:

Here is my UIButton subclass:

//  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似乎只能运行一次)

When my touch began on an UIButton I get every NSLog from the subclass but the main touchesMoved is executed only once. (self.nextResponder seems to works only once)

是否进行了某些更改,使其无法在xcode 4.4或iOS 5.1.1上运行?

Did something changed that make it not works on xcode 4.4 or iOS 5.1.1 ?

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

When I first tried to do that (on xcode 3.x and iOS 4.x) i found the solution here:Is there a way to pass touches through on the iPhone?

但是它不再起作用了……

But it not works anymore...

你能帮我吗?

非常感谢.

问题是使用[超级触摸...]代替,或者在[self.nextResponder触摸...]中使用更多的问题

EDIT : The problem is the same using [super touches...] instead or in more of [self.nextResponder touches...]

推荐答案

我之前已经解决了这个问题,不记得如何解决,但是文件中存在差异,因此我将在此处编写适用的新版本.

I have solved the problem a while ago and does not remember how but there are differences in my files so I'll write here the new version that works.

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

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

In each view I need this behavior, I place a Pan Gesture Recognizer in the file.xib that I link to the action panRecognizer: defined in the 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上也可以使用

FYI : This works also on XCode5 and SDK6.1

这篇关于使用UIButton xcode 4.4 iOS 5.1.1进行TouchesMoved的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:27