ios开发笔记.docx
- 文档编号:8798524
- 上传时间:2023-05-15
- 格式:DOCX
- 页数:80
- 大小:566.73KB
ios开发笔记.docx
《ios开发笔记.docx》由会员分享,可在线阅读,更多相关《ios开发笔记.docx(80页珍藏版)》请在冰点文库上搜索。
ios开发笔记
iphone开发笔记
退回输入键盘
- (BOOL)textFieldShouldReturn:
(id)textField{
[textField resignFirstResponder];
}
CGRect
CGRectframe=CGRectMake(origin.x,origin.y,size.width,size.height);矩形
NSStringFromCGRect(someCG)把CGRect结构转变为格式化字符串;
CGRectFromString(aString)由字符串恢复出矩形;
CGRectInset(aRect)创建较小或较大的矩形(中心点相同),+较小 -较大
CGRectIntersectsRect(rect1,rect2)判断两矩形是否交叉,是否重叠
CGRectZero高度和宽度为零的/位于(0,0)的矩形常量
CGPoint&CGSize
CGPointaPoint=CGPointMake(x,y);
CGSizeaSize=CGSizeMake(width,height);
设置透明度
[myViewsetAlpha:
value]; (0.0 设置背景色 [myViewsetBackgroundColor: [UIColorredColor]]; (blackColor;darkGrayColor;lightGrayColor; whiteColor;grayColor;redColor;greenColor; blueColor;cyanColor;yellowColor; magentaColor;orangeColor;purpleColor; brownColor;clearColor;) 自定义颜色 UIColor*newColor=[[UIColoralloc] initWithRed: (float)green: (float)blue: (float)alpha: (float)]; 0.0~1.0 竖屏 320X480 横屏 480X320 状态栏高(显示时间和网络状态) 20像素 导航栏、工具栏高(返回) 44像素 隐藏状态栏 [[UIApplicationshareApplication]setStatusBarHidden: YESanimated: NO] 横屏 [[UIApplicationshareApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight]. 屏幕变动检测 orientation==UIInterfaceOrientationLandscapeLeft 全屏 window=[[UIWindowalloc]initWithFrame: [UIScreenmainScreen]bounds]; 自动适应父视图大小: aView.autoresizingSubviews=YES; aView.autoresizingMask=(UIViewAutoresizingFlexibleWidth| UIViewAutoresizingFlexibleHeight); 定义按钮 UIButton*scaleUpButton=[UIButtonbuttonWithType: UIButtonTypeRoundedRect]; [scaleUpButtonsetTitle: @"放大"forState: UIControlStateNormal]; scaleUpButton.frame=CGRectMake(40,420,100,40); [scaleUpButtonaddTarget: self action: @selector(scaleUp) forControlEvents: UIControlEventTouchUpInside]; 设置视图背景图片 UIImageView*aView; [aViewsetImage: [UIImageimageNamed: @”name.png”]]; view1.backgroundColor=[UIColorcolorWithPatternImage: [UIImageimageNamed: @"image1.png"]]; 自定义UISlider的样式和滑块 我们使用的是UISlider的setMinimumTrackImage,和setMaximumTrackImage方法来定义图片的,这两个方法可以设置滑块左边和右边的图片的,不过如果用的是同一张图片且宽度和控件宽度基本一致,就不会有变形拉伸的后果,先看代码,写在viewDidLoad中: //左右轨的图片 UIImage*stetchLeftTrack=[UIImageimageNamed: @"brightness_bar.png"]; UIImage*stetchRightTrack=[UIImageimageNamed: @"brightness_bar.png"]; //滑块图片 UIImage*thumbImage=[UIImageimageNamed: @"mark.png"]; UISlider*sliderA=[[UISlideralloc]initWithFrame: CGRectMake(30,320,257,7)]; sliderA.backgroundColor=[UIColorclearColor]; sliderA.value=1.0; sliderA.minimumValue=0.7; sliderA.maximumValue=1.0; [sliderAsetMinimumTrackImage: stetchLeftTrackforState: UIControlStateNormal]; [sliderAsetMaximumTrackImage: stetchRightTrackforState: UIControlStateNormal]; //注意这里要加UIControlStateHightlighted的状态,否则当拖动滑块时滑块将变成原生的控件 [sliderAsetThumbImage: thumbImageforState: UIControlStateHighlighted]; [sliderAsetThumbImage: thumbImageforState: UIControlStateNormal]; //滑块拖动时的事件 [sliderAaddTarget: selfaction: @selector(sliderValueChanged: )forControlEvents: UIControlEventValueChanged]; //滑动拖动后的事件 [sliderAaddTarget: selfaction: @selector(sliderDragUp: )forControlEvents: UIControlEventTouchUpInside]; [self.viewaddSubview: sliderA]; 为了大家实验方便,我附上背景图brightness_bar.png和滑块图mark.png -(IBAction)sliderValueChanged: (id)sender{ UISlider*slider=(UISlider*)sender; NSString*newText=[[NSStringalloc]initWithFormat: @”%d”,(int)(slider.value+0.5f)]; label.text=newText; } 活动表单 -(IBActive)someButtonPressed: (id)sender { UIActionSheet*actionSheet=[[UIActionSheetalloc] initWithTitle: @”Areyousure? ” delegate: self cancelButtonTitle: @”Noway! ” destructiveButtonTitle: @”Yes,I’mSure! ” otherButtonTitles: nil]; [actionSheetshowInView: self.view]; [actionSheetrelease]; } 警告视图 -(void)actionSheet: (UIActionSheet*)actionSheetdidDismissWithButtonIndex: (NSInteger)buttonIndex { if(buttonIndex! =[actionSheetcancelButtonIndex]) { NSString*message=[[NSStringalloc]initWithFormat: @”Youcan breatheeasy,everythingwentOK.”]; UIAlertView*alert=[[UIAlertViewalloc] initWithTitle: @”Somethingwasdone” message: message delegate: self cancelButtonTitle: @”OK” otherButtonTitles: nil]; [alertshow]; [alertrelease]; [messagerelease]; } } 动画效果 -(void)doChange: (id)sender { if(view2==nil) { [selfloadSec]; } [UIViewbeginAnimations: nilcontext: NULL]; [UIViewsetAnimationDuration: 1]; [UIViewsetAnimationTransition: ([view1superview]? UIViewAnimationTransitionFlipFromLeft: UIViewAnimationTransitionFlipFromRight)forView: self.viewcache: YES]; if([view1superview]! =nil) { [view1removeFromSuperview]; [self.viewaddSubview: view2]; }else{ [view2removeFromSuperview]; [self.viewaddSubview: view1]; } [UIViewcommitAnimations]; } TableView #pragmamark- #pragmamarkTableViewDataSourceMethods //指定分区中的行数,默认为1 -(NSInteger)tableView: (UITableView*)tableView numberOfRowsInSection: (NSInteger)section { return[self.listDatacount]; } //设置每一行cell显示的内容 -(UITableViewCell*)tableView: (UITableView*)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath { staticNSString*SimpleTableIndentifier=@"SimpleTableIndentifier"; UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier: SimpleTableIndentifier]; if(cell==nil){ cell=[[[UITableViewCellalloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: SimpleTableIndentifier] autorelease]; } UIImage*image=[UIImageimageNamed: @"13.gif"]; cell.imageView.image=image; NSUIntegerrow=[indexPathrow]; cell.textLabel.text=[listDataobjectAtIndex: row]; cell.textLabel.font=[UIFontboldSystemFontOfSize: 20]; if(row<5) cell.detailTextLabel.text=@"Bestfriends"; else cell.detailTextLabel.text=@"friends"; returncell; } 图像、文本标签和详细文本标签 图像: 如果设置图像,则它显示在文本的左侧;文本标签: 这是单元的主要文本(UITableViewCellStyleDefault只显示文本标签);详细文本标签: 这是单元的辅助文本,通常用作解释性说明或标签 UITableViewCellStyleSubtitle UITableViewCellStyleDefault UITableViewCellStyleValue1 UITableViewCellStyleValue2 #pragmamark- #pragmamarkTableViewDelegateMethods //把每一行缩进级别设置为其行号 -(NSInteger)tableView: (UITableView*)tableViewindentationLevelForRowAtIndexPath: (NSIndexPath*)indexPath { NSUIntegerrow=[indexPathrow]; returnrow; } //获取传递过来的indexPath值 -(NSIndexPath*)tableView: (UITableView*)tableViewwillSelectRowAtIndexPath: (NSIndexPath*)indexPath { NSUIntegerrow=[indexPathrow]; if(row==0) returnnil; returnindexPath; } -(void)tableView: (UITableView*)tableViewdidSelectRowAtIndexPath: (NSIndexPath*)indexPath { NSUIntegerrow=[indexPathrow]; NSString*rowValue=[listDataobjectAtIndex: row]; NSString*message=[[NSStringalloc]initWithFormat: @"Youselected%@",rowValue]; UIAlertView*alert=[[UIAlertViewalloc]initWithTitle: @"RowSelected" message: message delegate: nil cancelButtonTitle: @"Yes,Idid! " otherButtonTitles: nil]; [alertshow]; [alertrelease]; [messagerelease]; [tableViewdeselectRowAtIndexPath: indexPathanimated: YES]; } //设置行的高度 -(CGFloat)tableView: (UITableView*)tableViewheightForRowAtIndexPath: (NSIndexPath*)indexPath { return40; } NavigationController推出push推出pop [self.navigationControllerpushViewController: _detailControlleranimated: YES]; [self.navigationControllerpopViewControllerAnimated: YES]; Debug: NSLog(@"%s%d",__FUNCTION__,__LINE__); 点击textField外的地方回收键盘 先定义一个UIControl类型的对象,在上面可以添加触发事件,令SEL实践为回收键盘的方法,最后将UIControl的实例加到当前View上。 UIControl*m_control=[[UIControlalloc]initWithFrame: CGRectMake(0,0,320,480)]; [m_controladdTarget: selfaction: @selector(keyboardReturn) forControlEvents: UIControlEventTouchUpInside]; [self.viewaddSubview: m_control]; -(void)keyboardReturn { [aTextFieldresignFirstResponder]; } 键盘覆盖输入框 当键盘调出时将输入框覆盖时,可以用下方法: -(BOOL)textFieldShouldBeginEditing: (UITextField*)textField { [self.viewsetFrame: CGRectMake(0,-100,320,480)]; returnYES; } -(BOOL)textFieldShouldEndEditing: (UITextField*)textField { [self.viewsetFrame: CGRectMake(0,0,320,480)]; returnYES; } 当准备输入时,将视图的位置上调100,这样键盘就不能覆盖到输入框。 当依赖注入方法不好使时,可以在AppDelegate内申明一个全局的控制器实例_anotherViewController,在另一个需要使用_anotherViewController的地方定义以下委托方法,使用共享的UIApplication实例来获取该委托的引用 SomeAppDelegate*appDelegate=(SomeAppDelegate*)[[UIApplicationsharedApplication]delegate]; _anotherViewController=appDelegate._anotherViewController; UIViewController内建TableView 纯代码在UIViewController控制器内建TableView @interfaceRootViewController: UIViewController NSArray*timeZoneNames; } @property(nonatomic,retain)NSArray*timeZoneNames; @end (void)loadView { UITableView*tableView=[[UITableViewalloc]initWithFrame: [[UIScreenmainScreen]applicationFrame]]style: UITableViewStylePlain]; tableView.autoresizingMask=(UIViewAutoresizingFlexibleHeight|UIViewAutoresizingWidth); tableView.delegate=self; tableView.dataSource=self; [tableViewreloadData]; self.view=tableView; [tableViewrelease]; } 将plist文件中的数据赋给数组 NSString*thePath=[[NSBundlemainBundle]pathForResource: @"States"ofType: @"plist"]; NSArray*array=[NSArrayarrayWithContentsOfFile: thePath]; UITouch 手指的触摸范围: 64X64 #pragmama
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ios 开发 笔记
![提示](https://static.bingdoc.com/images/bang_tan.gif)