У меня есть приложение для iPhone с ScrollView, которое должно реагировать на сенсорные события.
Чтобы поддерживать сенсорные события в просмотре прокрутки, мне нужно было создать его на основе собственного класса, который захватывал сенсорные события и передавал их в основной вид.
Работает как положено при запуске в симуляторе iPhone 4.3. С записями 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