iPhone开发入门

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

InitializingtheMoveMeApplicationiPhonemainXcodeiPhoneListing1MoveMemainmainmain.mmaintop-levelautoreleasepoolObjective-CautoreleaseUIApplicationMainMoveMeevent-processingListing1mainCreatingtheApplicationDelegateXcodeDelegateMoveMedelegateMoveMeAppDelegate.hMoveMeAppDelegate.mdelegateUIApplicationdelegateURLsintmain(intargc,char*argv[]){NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];intretVal=UIApplicationMain(argc,argv,nil,nil);[poolrelease];returnretVal;}delegateCreatingtheApplicationWindowdelegateshutdownnibdelegatenibfreeze-driedInterfaceBuildernibnibnib-loadingnibMoveMenibMoveMeAppDelegateUIApplicationMainWindow.xibnibFile’sOwnerUIApplicationconnectionsinspectorFigure3delegateFile’sOwnerproxyMoveMeAppDelegatenib-loadingFigure3TheapplicationdelegateCreatingtheApplicationWindow-(void)applicationDidFinishLaunching:(UIApplication*)application{/*SetupaviewcontrollertomanagetheMoveMeView.Sincetheviewcontrollerhasnocustombehaviorinthisapplication,justuseaninstanceofUIViewController.*/UIViewController*aViewController=[[UIViewControlleralloc]initWithNibName:@MoveMeViewbundle:[NSBundlemainBundle]];self.viewController=aViewController;[aViewControllerrelease];//Addtheviewcontroller'sviewasasubviewofthewindowUIView*controllersView=[viewControllerview];[windowaddSubview:controllersView];[windowmakeKeyAndVisible];}-(void)setUpPlacardView{//Createtheplacardview--itsinitmethodcalculatesitsframebasedonitsimagePlacardView*aPlacardView=[[PlacardViewalloc]init];self.placardView=aPlacardView;[aPlacardViewrelease];placardView.center=self.center;[selfaddSubview:placardView];}-(void)drawRect:(CGRect)rect{//Drawtheplacardat0,0[placardImagedrawAtPoint:(CGPointMake(0.0,0.0))];/*Drawthecurrentdisplaystring.TypicallyyouwoulduseaUILabel,butthisexampleservestoillustratetheUIKitextensionstoNSString.Thetextisdrawncenteroftheviewtwice-firstslightlyoffsetinblack,theninwhite--togiveanembossedappearance.ThesizeofthefontandtextarecalculatedinsetupNextDisplayString.*///FindpointatwhichtodrawthestringsoitwillbeinthecenteroftheviewCGFloatx=self.bounds.size.width/2-textSize.width/2;CGFloaty=self.bounds.size.height/2-textSize.height/2;CGPointpoint;//GetthefontoftheappropriatesizeUIFont*font=[UIFontsystemFontOfSize:fontSize];[[UIColorblackColor]set];point=CGPointMake(x,y+0.5);[currentDisplayStringdrawAtPoint:pointforWidth:(self.bounds.size.width-STRING_INDENT)withFont:fontfontSize:fontSizelineBreakMode:UILineBreakModeMiddleTruncationbaselineAdjustment:UIBaselineAdjustmentAlignBaselines];[[UIColorwhiteColor]set];point=CGPointMake(x,y);[currentDisplayStringdrawAtPoint:pointforWidth:(self.bounds.size.width-STRING_INDENT)withFont:fontfontSize:fontSizelineBreakMode:UILineBreakModeMiddleTruncationbaselineAdjustment:UIBaselineAdjustmentAlignBaselines];}-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event;-(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event;-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event;-(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event{//Weonlysupportsingletouches,soanyObjectretrievesjustthattouchfromtouchesUITouch*touch=[touchesanyObject];//Onlymovetheplacardviewifthetouchwasintheplacardviewif([touchview]!=placardView){//Incaseofadoubletapoutsidetheplacardview,updatetheplacard'sdisplaystringif([touchtapCount]==2){[placardViewsetupNextDisplayString];}return;}//AnimatethefirsttouchCGPointtouchPoint=[touchlocationInView:self];[selfanimateFirstTouchAtPoint:touchPoint];}-(void)touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event{UITouch*touch=[touchesanyObject];//IfthetouchwasintheplacardView,movetheplacardViewtoitslocationif([touchview]==placardView){CGPointlocation=[touchlocationInView:self];placardView.center=location;return;}}-(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event{UITouch*touch=[touchesanyObject];//IfthetouchwasintheplacardView,bounceitbacktothecenterif([touchview]==placardView){//Disableuserinteractionsosubsequenttouchesdon'tinterferewithanimationself.userInteractionEnabled=NO;[selfanimatePlacardViewToCenter];return;}}-(void)animateFirstTouchAtPoint:(CGPoint)touchPoint{/*Pulsetheplacardviewbyscalingupthendown,thenmovetheplacardtounderthefinger.ThisillustratesusingUIView'sbuilt-inanimation.Wewant,though,toanimatethesameproperty(transform)twice--firsttoscaleup,thentoshrink.Youcan'tanimatethesamepropertymorethanonceusingthebuilt-inanimation--thelastonewins.Sowe'llsetadelegateactiontobeinvokedafterthefirstanimationhasfinished.Itwillcompletethesequence.Notethatwecanpassinformation--inthiscase,theusingthecontext.Thecontextneedstobeapointer.AconvenientwaytopassaCGPointhereistowrapitinanNSValueobject.However,thevaluereturnedfromvalueWithCGPointisautoreleased.Normallythiswouldn'tbeanissuebecausetypicallyifyouneedtousethevaluelateryoustoreitasaninstancevariableusinganaccessormethodthatretainsit,orpassittoanotherobjectwhichretainsit.Inthiscase,though,it'sbeingpassedasavoid*parameter,andit'snotretainedbytheUIViewclass.Bythetimethedelegatemethodiscalled,therefore,theautoreleasepo

1 / 20
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功