ウィンドウに単一の UIScrollView を追加するベアボーン iOS アプリケーションを作成しました (ViewControllers または UIViews はありません。何が起こっているのかを理解できるかどうかを確認するためにこれを行いました)。
私が欲しいのは、デバイスが揺れたときに UIScrollView に通知することだけです。
canBecomeFirstResponder メソッドを実装するために UIScrollView をサブクラス化しました。
#import "SampleScrollView.h"
@implementation SampleScrollView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
そして、AppDelegate で、SampleScrollView のオブジェクトを作成し、ウィンドウに追加してから、最初のレスポンダーに設定しようとします。
#import "SampleScrollView.h"
@implementation ResponderTestAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
CGRect thisFrame = [[self window] bounds];
SampleScrollView *myScrollView = [[SampleScrollView alloc] initWithFrame:thisFrame];
// Set scrollview to be the first responder
BOOL success = [myScrollView becomeFirstResponder];
if (success)
{
NSLog(@"myScrollView is first responder");
}
else
{
NSLog(@"Not first responder.");
}
これは非常に単純化された例ですが、何らかの理由でアプリケーションに SampleScrollView オブジェクトをファーストレスポンダーとして報告させることができません。私は非常に単純なものが欠けていると確信していますよね?