TouchMoved 在 iPhone 的 iOS5 中未针对 UIScrollView 触发

发布于 2024-12-10 14:58:30 字数 5883 浏览 0 评论 0原文

我有一个带有 ScrollView 的 iPhone 应用程序,它需要响应触摸事件。

为了支持滚动视图中的触摸事件,我需要将其基于一个自定义类,该类捕获触摸事件并将它们传递到主视图。

在 4.3 iPhone 模拟器中运行时,它可以按预期工作。通过 NSLOG 条目,我得到如下输出:

2011-10-17 09:28:06.782 SingleView[7000:b303] gameTable began
2011-10-17 09:28:06.784 SingleView[7000:b303] outside began
2011-10-17 09:28:09.976 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:09.976 SingleView[7000:b303] outside moved
2011-10-17 09:28:09.977 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:09.978 SingleView[7000:b303] outside moved
2011-10-17 09:28:10.019 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:10.020 SingleView[7000:b303] outside moved
2011-10-17 09:28:10.046 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:10.047 SingleView[7000:b303] outside moved
2011-10-17 09:28:10.077 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:10.077 SingleView[7000:b303] outside moved
2011-10-17 09:28:10.143 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:10.144 SingleView[7000:b303] outside moved
2011-10-17 09:28:12.433 SingleView[7000:b303] gameTable ended
2011-10-17 09:28:12.434 SingleView[7000:b303] outside ended

gameTable 是自定义类内部的事件,它只是将其传递到外部视图。

如果您在 iOS 5 iPhone 模拟器中运行相同的应用程序,您会得到如下输出:

2011-10-17 09:41:46.319 SingleView[7077:f803] gameTable began
2011-10-17 09:41:46.322 SingleView[7077:f803] outside began
2011-10-17 09:41:47.851 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.852 SingleView[7077:f803] outside moved
2011-10-17 09:41:47.853 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.860 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.893 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.916 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.945 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:48.009 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:48.062 SingleView[7077:f803] gameTable moved

请注意,外部移动事件仅触发一次。

这是代码。这是 Xcode 中的单一视图项目。

//
//  XYZAppDelegate.h
//  SingleView
//

#import <UIKit/UIKit.h>

@class XYZViewController;

@interface XYZAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) XYZViewController *viewController;

@end



//
//  XYZAppDelegate.m
//  SingleView
//

#import "XYZAppDelegate.h"

#import "XYZViewController.h"

@implementation XYZAppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[XYZViewController alloc] initWithNibName:@"XYZViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

@end


//
//  XYZViewController.h
//  SingleView
//

#import <UIKit/UIKit.h>
#import "GameTableScrollView.h"

@interface XYZViewController : UIViewController <UIScrollViewDelegate> {
    IBOutlet GameTableScrollView *outsideScrollView;

}

@property (nonatomic, retain) GameTableScrollView *outsideScrollView;

@end

    //
    //  XYZViewController.m
    //  SingleView
    //

    #import "XYZViewController.h"

    @implementation XYZViewController
    @synthesize outsideScrollView;

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        [outsideScrollView setScrollEnabled:NO];
        [outsideScrollView setContentSize:CGSizeMake(480,555)];
    }


    // Handles the start of a touch
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"outside began");
    }

    // Handles the continuation of a touch.
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {  
        NSLog(@"outside moved");
    }


    // Handles the end of a touch event.
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"outside ended");
    }

    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"outside cancelled");
    }

    @end

    //
    //  GameTableScrollView.h
    //

    #import <UIKit/UIKit.h>

    @interface GameTableScrollView : UIScrollView <UIScrollViewDelegate> {

    }

    @end

//
//  GameTableScrollView.m
//

#import "GameTableScrollView.h"

@implementation GameTableScrollView

#pragma mark - Touch 

// Handles the start of a touch
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"gameTable began");
    if (!self.dragging) {
        [self.nextResponder touchesBegan:touches withEvent:event];
    }
    else [super touchesBegan:touches withEvent:event];
}


// Handles the continuation of a touch.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
    NSLog(@"gameTable moved");
    if (!self.dragging) {
        [self.nextResponder touchesMoved:touches withEvent:event];
    }
    else [super touchesMoved:touches withEvent:event];
}


// Handles the end of a touch event.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"gameTable ended");
    if (!self.dragging) {
        [self.nextResponder touchesEnded:touches withEvent:event];
    } 
    else [super touchesEnded:touches withEvent:event];
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"gameTable cancelled");
    if (!self.dragging) {
        [self.nextResponder touchesCancelled:touches withEvent:event];
    } 
    else [super touchesCancelled:touches withEvent:event];
}

@end

I have an iPhone app with a ScrollView which needs to respond to touch events.

In order to support touch events in the scrollview I needed to base it off a custom class which captured the touch events and passed them to the main view.

It works as expected when run in the 4.3 iPhone simulator. With the NSLOG entries I get output like this:

2011-10-17 09:28:06.782 SingleView[7000:b303] gameTable began
2011-10-17 09:28:06.784 SingleView[7000:b303] outside began
2011-10-17 09:28:09.976 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:09.976 SingleView[7000:b303] outside moved
2011-10-17 09:28:09.977 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:09.978 SingleView[7000:b303] outside moved
2011-10-17 09:28:10.019 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:10.020 SingleView[7000:b303] outside moved
2011-10-17 09:28:10.046 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:10.047 SingleView[7000:b303] outside moved
2011-10-17 09:28:10.077 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:10.077 SingleView[7000:b303] outside moved
2011-10-17 09:28:10.143 SingleView[7000:b303] gameTable moved
2011-10-17 09:28:10.144 SingleView[7000:b303] outside moved
2011-10-17 09:28:12.433 SingleView[7000:b303] gameTable ended
2011-10-17 09:28:12.434 SingleView[7000:b303] outside ended

The gameTable is the event inside the custom class which just passes it to the outside view.

If you run the same app in the iOS 5 iPhone simulator you get output like this:

2011-10-17 09:41:46.319 SingleView[7077:f803] gameTable began
2011-10-17 09:41:46.322 SingleView[7077:f803] outside began
2011-10-17 09:41:47.851 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.852 SingleView[7077:f803] outside moved
2011-10-17 09:41:47.853 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.860 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.893 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.916 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:47.945 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:48.009 SingleView[7077:f803] gameTable moved
2011-10-17 09:41:48.062 SingleView[7077:f803] gameTable moved

Notice the outside moved event only fires once.

Here is the code. It's a single view project in Xcode.

//
//  XYZAppDelegate.h
//  SingleView
//

#import <UIKit/UIKit.h>

@class XYZViewController;

@interface XYZAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) XYZViewController *viewController;

@end



//
//  XYZAppDelegate.m
//  SingleView
//

#import "XYZAppDelegate.h"

#import "XYZViewController.h"

@implementation XYZAppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[XYZViewController alloc] initWithNibName:@"XYZViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

@end


//
//  XYZViewController.h
//  SingleView
//

#import <UIKit/UIKit.h>
#import "GameTableScrollView.h"

@interface XYZViewController : UIViewController <UIScrollViewDelegate> {
    IBOutlet GameTableScrollView *outsideScrollView;

}

@property (nonatomic, retain) GameTableScrollView *outsideScrollView;

@end

    //
    //  XYZViewController.m
    //  SingleView
    //

    #import "XYZViewController.h"

    @implementation XYZViewController
    @synthesize outsideScrollView;

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.

        [outsideScrollView setScrollEnabled:NO];
        [outsideScrollView setContentSize:CGSizeMake(480,555)];
    }


    // Handles the start of a touch
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"outside began");
    }

    // Handles the continuation of a touch.
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {  
        NSLog(@"outside moved");
    }


    // Handles the end of a touch event.
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"outside ended");
    }

    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"outside cancelled");
    }

    @end

    //
    //  GameTableScrollView.h
    //

    #import <UIKit/UIKit.h>

    @interface GameTableScrollView : UIScrollView <UIScrollViewDelegate> {

    }

    @end

//
//  GameTableScrollView.m
//

#import "GameTableScrollView.h"

@implementation GameTableScrollView

#pragma mark - Touch 

// Handles the start of a touch
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"gameTable began");
    if (!self.dragging) {
        [self.nextResponder touchesBegan:touches withEvent:event];
    }
    else [super touchesBegan:touches withEvent:event];
}


// Handles the continuation of a touch.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
    NSLog(@"gameTable moved");
    if (!self.dragging) {
        [self.nextResponder touchesMoved:touches withEvent:event];
    }
    else [super touchesMoved:touches withEvent:event];
}


// Handles the end of a touch event.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"gameTable ended");
    if (!self.dragging) {
        [self.nextResponder touchesEnded:touches withEvent:event];
    } 
    else [super touchesEnded:touches withEvent:event];
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"gameTable cancelled");
    if (!self.dragging) {
        [self.nextResponder touchesCancelled:touches withEvent:event];
    } 
    else [super touchesCancelled:touches withEvent:event];
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美胚控场 2024-12-17 14:58:30

我最近发布了一个类似的答案到类似的

在 GameTableScrollView 中,您有以下代码:

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

要解决此问题,您需要更改此行:

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

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

将导致 GameTableScrollView 将触摸事件传递到外部视图。

I recently posted an answer to a similar question.

In GameTableScrollView you have this code:

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

And to fix the problem you need to change this line:

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

To this:

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

This will cause the GameTableScrollView to pass the touch event to the outside view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文