6 T o u c h i n g t h e i P h o n e S D K 3. 0
6.1
01: -(void) touchesbegan:(nsset * ) touches withevent:(uievent * )event { 02: NSSet * alltouches = [event alltouches]; 03: if ([alltouches count]>1) { 04: [self.nextresponder touchesbegan:touches withevent:event]; 05: } else { 06: // : 07: } 08: } 6.2 touchesbegan:(nsset * )touches withevent:(uievent * )event;
touchesmoved:(nsset * )touches withevent:(uievent * )event
touchesended:(nsset * )touches withevent:(uievent * )event
6.3
<TouchView.h> 01: #import <UIKit/UIKit.h> 02: 03: @interface TouchView : UIView { 04: NSArray * imgviewarray; 05: UILabel * label; 06: } 07: -(void) putfingertips:(nsarray * ) touches remove:(nsset * ) removes; 08: 09: @property (nonatomic, retain) IBOutlet UILabel * label; 10: @end
<TouchView.m> 01: - (id)initwithcoder:(nscoder * )decoder { 02: if (self = [super initwithcoder:decoder]) { 03: UIImage * imgball = [[UIImage alloc] initwithcontentsoffile: [[NSBundle mainbundle] pathforresource:@ Mark oftype:@ png ]]; 04: imgviewarray = [[NSArray arraywithobjects: 05: [[UIImageView alloc] initwithimage:imgball], 06: [[UIImageView alloc] initwithimage:imgball], 07: [[UIImageView alloc] initwithimage:imgball], 08: [[UIImageView alloc] initwithimage:imgball], 09: [[UIImageView alloc] initwithimage:imgball],nil 10: ] retain]; 11: [imgball release]; 12: for (UIImageView * imgview in imgviewarray) { 13: [imgview sethidden:yes]; 14: [imgview setuserinteractionenabled:no]; 15: [self addsubview:imgview]; 16: } 17: } 18: return self; 19: } 20: - (void)touchesbegan:(nsset * )touches withevent:(uievent * )event { 21: [self putfingertips:[[event alltouches] allobjects] remove:nil]; 22: label.text = [NSString stringwithformat:@ Began:%d (total:%d touch), 23: [touches count],[[event alltouches]count]]; 24: } 25: - (void)touchescancelled:(nsset * )touches withevent:(uievent * )event { 26: [self putfingertips:nil remove:nil]; 27: label.text = [NSString stringwithformat:@ Cancelled:%d (total%d touch), 28: [touches count],[[event alltouches]count]]; 29: } 30: - (void)touchesended:(nsset * )touches withevent:(uievent * )event { 31: [self putfingertips:[[event alltouches] allobjects] remove:touches]; 32: label.text = [NSString stringwithformat:@ Ended:%d (total:%d touch), 33: [touches count],[[event alltouches]count]]; 34: } 35: - (void)touchesmoved:(nsset * )touches withevent:(uievent * )event { 36: [self putfingertips:[[event alltouches] allobjects] remove:nil]; 37: label.text = [NSString stringwithformat:@ Moved:%d (total:%d touch), 38: [touches count],[[event alltouches]count]]; 39: }
40: -(void) putfingertips:(nsarray * ) touches remove:(nsset * ) removes { 41: for (int i=0;i<[imgviewarray count];i++) { 42: [[imgviewarray objectatindex:i] sethidden:yes]; 43: if (i < [touches count]) { 44: if ([removes containsobject:[touches objectatindex:i]]) { 45: } else { 46: [[imgviewarray objectatindex:i] sethidden:no]; 47: [[imgviewarray objectatindex:i] setcenter: [[touches objectatindex:i] locationinview:self]]; 48: } 49: } 50: } 51: }
<TouchView.m> 01: - (void)touchesbegan:(nsset * )touches withevent:(uievent * )event { 02: [self putfingertips:[[event alltouches] allobjects] remove:nil]; 03: int tapcount = [[touches anyobject] tapcount]; 04: if ( tapcount > 1) { 05: labeltap.text = [NSString stringwithformat:@ Tabbed : %d,tapcount]; 06: } else { 07: labeltap.text = nil; 08: }
09: label.text = [NSString stringwithformat:@ Began:%d (total:%d touch), 10: [touches count],[[event alltouches]count]]; 11: } 12: - (void)touchesended:(nsset * )touches withevent:(uievent * )event { 13: [self putfingertips:[[event alltouches] allobjects] remove:touches]; 14: label.text = [NSString stringwithformat:@ Ended:%d (total:%d touch), 15: [touches count],[[event alltouches]count]]; 16: int tapcount = [[touches anyobject] tapcount]; 17: if ( tapcount == 2) { 18: //. 19: [self performselector:@selector(showallcircles) withobject:nil afterdelay:0.3]; 20: NSLog(@ Scheduled ); 21: } 22: }
01: - (void)touchesbegan:(nsset * )touches withevent:(uievent * )event { 02:...... 03: if (tapcount > 2) { 04: [UIView cancelpreviousperformrequestswithtarget:self]; 05: NSLog(@ Cancelled ); 06: } 07: }
6.5 <TouchView.m> 01: - (void)touchesbegan:(nsset * )touches withevent:(uievent * )event { 02:...... 03: // (.) 04: if ([[event alltouches] count]==1) { 05: startpoint = [[touches anyobject] locationinview:self]; 06: mayswiped = YES; 07: } else { 08: mayswiped = NO; 09: } 10: } 11: #define kswipe_vert_threshold 5 12: #define kswipe_horz_threshold 30 13: 14: - (void)touchesmoved:(nsset * )touches withevent:(uievent * )event { 15: 16: // ( )
17: if (mayswiped) { 18: CGPoint point = [[touches anyobject] locationinview:self]; 19: //? 20: if ( fabsf( point.x - startpoint.x ) > kswipe_horz_threshold ) { 21: //? 22: if ( fabsf( point.y - startpoint.y ) < kswipe_vert_threshold) { 23: [self performselector:@selector(showallcircles) withobject:nil afterdelay:0.3]; 24: NSLog(@ Scheduled ); 25: } 26: } 27: } 28: } 31: - (void)touchesended:(nsset * )touches withevent:(uievent * )event { 32:...... 33: // 34: mayswiped = NO; 35: }
01: - (void)touchesbegan:(nsset * )touches withevent:(uievent * )event { 02:...... 03: if ([[event alltouches] count]==2) { 04: CGPoint pta = [ [[[event alltouches] allobjects] objectatindex:0] locationinview:self]; 05: CGPoint ptb = [ [[[event alltouches] allobjects] objectatindex:1] locationinview:self]; 06: distance = [self distancetwopoint:pta to:ptb]; 07: } 08: } 10: - (void)touchesmoved:(nsset * )touches withevent:(uievent * )event { 11:...... 12: if ([[event alltouches] count]==2) { 13: CGPoint pta = [ [[[event alltouches] allobjects] objectatindex:0] locationinview:self]; 14: CGPoint ptb = [ [[[event alltouches] allobjects] objectatindex:1] locationinview:self]; 15: float newdistance = [self distancetwopoint:pta to:ptb]; 16: if (distance==0) { 17: distance = newdistance; 18: } else if ( (newdistance - distance) > kpinch_threshold ) { 19: NSLog(@ ZoomIn ); 20: } else if ( (distance - newdistance) > kpinch_threshold ) { 21: NSLog(@ ZoomOut ); 22: } 23: } 24: } 25: - (void)touchescancelled:(nsset * )touches withevent:(uievent * )event { 26:...... 27: distance = 0; 28: } 29: 30: - (void)touchesended:(nsset * )touches withevent:(uievent * )event { 31:...... 32: distance = 0; 33: }
6.6
<FrameViewController.h> 01: #import <UIKit/UIKit.h> 02: 03: @interface FrameViewController : UIViewController <UIScrollViewDelegate> { 04: NSUndoManager * undomanager; 05: float oldzoom; 06: 07: UIImageView * imgview; 08: UIScrollView * scrlview; 09: } 10: 11: -(void) updatezoom:(nsnumber * )param; 12: 13: @property (nonatomic, retain) NSUndoManager * undomanager; 14: @property (nonatomic, retain) IBOutlet UIImageView * imgview; 15: @property (nonatomic, retain) IBOutlet UIScrollView * scrlview; 16: 17: @end <FrameViewController.m> 01: -(NSUndoManager * ) undomanager { 02: if (undomanager==nil) { 03: undomanager = [[NSUndoManager alloc] init]; 04: [undomanager setlevelsofundo:3]; 05: } 06: return undomanager; 07: }
08: - (void)scrollviewdidendzooming:(uiscrollview * )scrollview withview:(uiview * )view atscale:(float)scale { 09: if (scrollview.zoomscale!= oldzoom) { 10: [self updatezoom:[nsnumber numberwithfloat:scrollview.zoomscale]]; 11: } 12: [self becomefirstresponder]; 13: } 14: - (BOOL)canBecomeFirstResponder { 15: return YES; 16: } 17: -(void) updatezoom:(nsnumber * )param { 18: 19: scrlview.zoomscale = [param floatvalue]; 20: 21: [self.undomanager registerundowithtarget:self selector: @selector(updatezoom:) object:[nsnumber numberwithfloat:oldzoom]]; 22: if (![self.undomanager isundoing]) { 23: if (oldzoom < scrlview.zoomscale) 24: [self.undomanager setactionname:@ ]; 25: else 26: [self.undomanager setactionname:@ ]; 27: } 28: oldzoom = scrlview.zoomscale; 29: }
<FrameAppDelegate.m> 01: - (void)applicationdidfinishlaunching:(uiapplication * )application { 02: 03: application.applicationsupportsshaketoedit = YES; 04: 05: // Override point for customization after app launch 06: [window addsubview:viewcontroller.view]; 07: [window makekeyandvisible]; 08: }
6.7
<MyTextField.h> 01: #import <UIKit/UIKit.h> 02: 03: @interface MyTextField : UITextField { 04: 05: } 06: @end <CopyPasteViewController.m> 01: -(IBAction) onmenu { 02: if ([text becomefirstresponder]) { 03: UIMenuController * mc = [UIMenuController sharedmenucontroller]; 04: [mc settargetrect:cgrectzero inview:text]; 05: if (!mc.menuvisible) 06: [mc setmenuvisible:yes animated:yes]; 07: } 08: }
09: -(IBAction) onwrite { 10: UIPasteboard * board = [UIPasteboard generalpasteboard]; 11: board.string = @ TEST. ; 12: } 13: 14: -(IBAction) onclear { 15: UIPasteboard * board = [UIPasteboard generalpasteboard]; 16: board.items = nil; 17: } < MyTextField.m> 01: - (BOOL)canPerformAction:(SEL)action withsender:(id)sender { 02: NSLog(@ canperformaction:%s,action); 03: return [super canperformaction:action withsender:sender]; 04: }
[Session started at 2009-05-05 01:16:26 +0900.] 2009-05-05 01:16:32.054 [22969:20b] canperformaction:cut: 2009-05-05 01:16:32.055 [22969:20b] canperformaction:copy: 2009-05-05 01:16:32.055 [22969:20b] canperformaction:select: 2009-05-05 01:16:32.056 [22969:20b] canperformaction:selectall: 2009-05-05 01:16:32.056 [22969:20b] canperformaction:paste: 2009-05-05 01:16:32.056 [22969:20b] canperformaction:_setrtoltextdirection: 2009-05-05 01:16:32.057 [22969:20b] canperformaction:_setltortextdirection: 01: - (void)select:(id)sender { 02: [super select:sender]; 03: } 04: - (void)selectall:(id)sender { 05: [super selectall:sender]; 06: } 07: - (void)cut:(id)sender { 08: [super cut:sender]; 09: UIPasteboard * board = [UIPasteboard generalpasteboard]; 10: NSLog(@ cut : %@,board.string); 11: } 12: - (void)copy:(id)sender { 13: [super copy:sender]; 14: UIPasteboard * board = [UIPasteboard generalpasteboard]; 15: NSLog(@ copied : %@,board.string); 16: } 17: - (void)paste:(id)sender { 18: UIPasteboard * board = [UIPasteboard generalpasteboard]; 19: NSLog(@ pasted : %@,board.string); 20: [super paste:sender]; 21: }
6.8