ios开发总结

来源:互联网 发布:mac重装系统失败怎么办 编辑:程序博客网 时间:2024/05/06 16:25

3.方法flashScrollIndicators:这个很有用,闪一下滚动条,暗示是否有可滚动的内容。可以在ViewDidAppear[table reload]之后调用。 


设置线宽,如果是retina屏,lineWidth设为1,实际显示的宽度是2个像素,这里进行一下处理:  

#define SETLINEWIDTH(ctx,w) CGContextSetLineWidth(ctx, w/[UIScreen mainScreen].scale) 


(四)在CGContext中输出汉字:CGContextShowTextAtPoint是不支持汉字的,需要用NSStringdrawAtPointdrawInRect方法 

  

(五)一个不停震动的方法: 

// 定义一个回调函数,震动结束时再次发出震动 

void MyAudioServicesSystemSoundCompletionProc (SystemSoundID  ssID,void *clientData) 

      BOOL* iShouldKeepBuzzing = clientData; 

      if (*iShouldKeepBuzzing) {        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

      } else { 

           //Unregister, so we don't get called again... 

           AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate); 

      }  

  

以下为调用的代码: 

BOOL iShouldKeepBuzzing = YES; 

AudioServicesAddSystemSoundCompletion ( 

  kSystemSoundID_Vibrate,                                                                       

  NULL,                                                                                                    

  NULL,                                                                                                              

  MyAudioServicesSystemSoundCompletionProc,                                                 

&iShouldKeepBuzzing ); 

AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); 


(六)关于更新,iPhone自动保存document中的内容,如果你把文件放在document中,以后开发又改了这个文件的内容或格式,那更新之后运行很可能出错。解决的办法是,配置文件放bundle里,或者改个文件名。每次更新前都要从App store 下载旧版本,运行一段一时间后,再此基础上编译新版,运行不出错才能上传 


(十一)UIImage:stretchableImageWithLeftCapWidth:topCapHeight:有时图片模糊(blur)的原因:像素没有和device pixel对齐.使用instrumentCore Animation可以检测这个,勾选"color misaligned images",如果图片显示为红紫色,就是没有对齐


(十二)UIPopoverController如果是用presentPopoverFromBarButtonItem显示的,设备旋转时,popover可以自动调整位置;如果是用presentPopoverFromRect显示的,需要present again 

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

[aPopover presentPopoverFromRect:targetRect.frame inView:self.view permittedArrowDirecti*****:UIPopoverArrowDirectionAny animated:YES]; 


(十四)禁止textFieldtextView的复制粘贴菜单: 

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 

     if ([UIMenuController sharedMenuController]) { 

       [UIMenuController sharedMenuController].menuVisible = NO

     } 

     return NO


(十五)时间相关 

NSDate需要设置calendar,使用不方便也因为服务器传过来的是time_t格式,所以我在客户端对时间的操作主要用的C语言的方法。 

需要注意的是,有的函数不是线程安全的,也就是说在同一个范围内调用多次时,需要调用线程安全的版本,这样的函数有: 

localtime_r 

asctime_r 

ctime_r 

gmtime_r 

localtime_r 

另外,可以直接给struct tm各成员变量赋值,例如(注意顺序) 

struct tm tmStart = {second,minute,hour,day, mon, year}; 

struct tm的各成员是不能的加减的,因为超过了各变量的范围,可能出错,需要先转成time_t,再加减相应的时间


(十七)GestureRecognizer相关 

1.一个ViewGestureRecognizer又有按钮(或其它需要处理action event的控件)时,有时按钮不灵敏,解决办法: 

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 

     CGPoint pt      = [touch locationInView:baseView]; 

     UIView *btn     = [baseView viewWithTag:TAG_MYBTN]; 

     CGPoint ptInbtn = [baseView convertPoint:pt toView:btn]; 

     return ![btn pointInside:ptInbtn withEvent:nil]; 

  

2.实现某个view点一下就移除时,要防止移除两次。(此方法适用于希望GestureRecognizer只执行一次的情况) 

-(void)OnTapViewTobeRemoved:(UITapGestureRecognizer *)sender 

     if (!sender.enabled) { 

           return

     } 

     sender.enabled = NO

     [sender.view removeFromSuperview]; 


(二十五)category可以用来调试。除了隐藏私有方法外,我主要用它截住函数。 
1:测试时我想知道TableViewCell有没有释放,就可以这样写 
@implementation UITableViewCell(dealloc) 
-(void)dealloc 

NSLog(@"%@",NSStringFromSelector(_cmd)); 
  // allSubviewscookBook里的函数,可以取一个view的所有subView 
    NSArray *array = allSubviews(self); 
    NSLog(@"%@",array); 

    [super dealloc]; 

@end 
其它的类也可以这样写,你随便输出什么 
2:我调试程序,觉得table的大小变了,想找到在哪改变的,这样做: 
@implementation UITableView(setframe) 
-(void)setFrame:(CGRect)frame 

NSLog(%"%@",self); 
    [super setFrame: frame]; 

@end 

原创粉丝点击