2008-03-22 iPhone アプリケーション 備忘録
■タッチイベントの処理
システムはタッチイベントをUIResponderのインスタンスに送る。MoveMeは二つのビュークラスを実装しているが、MyViewのみがこのイベントメッセージに反応する。このクラスは、Welcomeボタンの外側の境界と内側の境界をURResponderの下記のメソッドをオーバーライドすることによって実装する。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
イベント処理をシンプルにするために、MoveMeでは最初の指のタッチだけを追っている。これはUIViewクラスによりマルチタッチイベントを不可にすることにより行われている。このようにすると連続的に違う指で操作しても無視する。詳しくはsetMultipleTouchEnabledのリファレンスを。さて、タッチイベント処理のためにMyViewクラスは下記のステップを踏む
・タッチがきたら、どこでそれが起きたかをチェック
・ボタンの内側で指が動いたらボタンの位置をアップデートする。
・もしも指がボタンの内部にあり、デバイスの表面を離れていくようであれば初期設定の場所に戻るアニメーションを表示
下記に示す3つの関数が一連の動作を担う
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // We only support single touches, so get the touch from allTouches UITouch *touch = [[event allTouches] anyObject]; // Only move the placard view if the touch was in the placard view if ([touch view] != placardView) { // In case of a double tap outside the placard view, update the placard's display string if ([touch tapCount] == 2) { [placardView setupNextDisplayString]; } return; } // Animate the first touch CGPoint touchPoint = [self convertPoint:[touch locationInView] fromView:placardView]; [self animateFirstTouchAtPoint:touchPoint]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; // If the touch was in the placardView, move the placardView to its location if ([touch view] == placardView) { CGPoint location = [touch locationInView]; location = [self convertPoint:location fromView:placardView]; placardView.center = location; return; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; // If the touch was in the placardView, bounce it back to the center if ([touch view] == placardView) { // Disable user interaction so subsequent touches don't interfere with animation self.userInteractionEnabled = NO; [self animatePlacardViewToCenter]; return; } }
トラックバック - http://d.hatena.ne.jp/Chabashira/20080322/1206175764
リンク元
- 3 http://www.google.co.jp/ig?hl=ja
- 2 http://d.hatena.ne.jp/keyword/iPhone
- 2 http://shuncolle.nifty.com/shuncolle/word/scipy.htm
- 2 http://translate.google.com/translate_p?hl=en&sl=ja&u=http://d.hatena.ne.jp/Chabashira/20080322&prev=/search?q=touchesEnded:(NSSet*)touches+withEvent:(UIEvent*)event&hl=en&sa=G
- 2 http://www.google.co.jp/search?hl=ja&rlz=1T4ADBR_jaJP235JP236&q=Xcode iphone+関数一覧&lr=
- 2 http://www.google.co.jp/search?q=UITouch&lr=lang_ja&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:ja-JP-mac:official&client=firefox-a
- 2 http://www.google.co.jp/search?sourceid=navclient&aq=t&hl=ja&ie=UTF-8&rlz=1T4GGIH_jaJP214JP214&q=UIView+transform
- 2 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4GGIH_jaJP214JP214&q=UIView+transform
- 1 http://blogsearch.google.co.jp/blogsearch?hl=ja&q=iphone&lr=lang_ja&ie=UTF-8&scoring=d
- 1 http://d.hatena.ne.jp/keyworddiary/iPhone?date=20080322
