美文网首页
ios开发一些小技巧(持续更新)

ios开发一些小技巧(持续更新)

作者: Arxu | 来源:发表于2016-08-22 14:45 被阅读32次

    1、隐藏导航栏的正确姿势

    套路一:在viewWillAppearviewWillDisappear方法里面用动画方式设置是否隐藏NavigationBar

    - (void)viewWillAppear:(BOOL)animated {
       [super viewWillAppear:animated];
       [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    - (void)viewWillDisappear:(BOOL)animated { 
      [super viewWillDisappear:animated];
      [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
    

    套路二:设置self为导航控制器的代理,实现代理方法,在将要显示控制器中设置导航栏隐藏和显示,使用这种方式不仅完美切合滑动返回手势,同时也解决了切换tabBar的时候,导航栏动态隐藏的问题.

    - (void)viewDidLoad { 
      [super viewDidLoad]; // 设置导航控制器的代理为
      self self.navigationController.delegate = self;
    }
    #pragma mark - UINavigationControllerDelegate// 将要显示控制器
    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 
      // 判断要显示的控制器是否是自己 
      BOOL isShowHomePage = [viewController isKindOfClass:[self class]]; 
      [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
    }
    

    2、设置图片的圆角

    设置圆角,我们也许会这么做

    self.iconImage.layer.masksToBounds = YES;
    self.iconImage.layer.cornerRadius = 20;
    

    如果只是设置一张图片的话,这样的设置对内存没什么大影响,如果是在一个TableView里面的,每个cell都有这样的圆角设置,就会因为使用图层过量而造成卡顿现象!


    可用以下套路避免卡顿,可把这个方法单独放到分类中使用

    - (UIImage *)cutCircleImage {
        UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
        // 获取上下文
        CGContextRef ctr = UIGraphicsGetCurrentContext();
        // 设置圆形
        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
        CGContextAddEllipseInRect(ctr, rect);
        // 裁剪
        CGContextClip(ctr);
        // 将图片画上去
        [self drawInRect:rect];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    3、以当前时间为基准进行时间的增减

    以当前时间为基准进行时间的增减

     *  增减时间(以当前时间为基准)
     *
     *  @param year  1为1年以后的日期 -1为年之前的日期 0为今年
     *  @param month 1为1个月以后的日期 -1为一个月之前的日期 0为本月
     *  @param day   1为1天以后的日期 -1为一天之前的日期 0为今天
     *
     *  @return 时间字符串
     */
    
    +(NSString*)getPastORFutureDateWithYear:(int)year month:(int)month day:(int)day
     {
         NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
         NSDateComponents *comps = nil;
         comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
         NSDateComponents *adcomps = [[NSDateComponents alloc] init];
         [adcomps setYear:year];
         [adcomps setMonth:month];
         [adcomps setDay:day];
         NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:[NSDate date] options:0];
         NSDateFormatter *formatter =  [[NSDateFormatter alloc] init];
         [formatter setDateFormat:@"yyyy-MM-dd"];
         NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
         [formatter setTimeZone:timeZone];
         NSString *dateFromData = [formatter stringFromDate:newdate];
         return dateFromData;
     }
    

    4、跳转到系统应用的URL

    通过[[UIApplication sharedApplication] openURL:url]来实现跳转系统的应用

    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:url]) 
    { 
        [[UIApplication sharedApplication] openURL:url];
    }
    

    常用的跳转URL还有以下
    About — prefs:root=General&path=About Accessibility — prefs:root=General&path=ACCESSIBILITY AirplaneModeOn— prefs:root=AIRPLANE_MODE Auto-Lock — prefs:root=General&path=AUTOLOCK Brightness — prefs:root=Brightness Bluetooth — prefs:root=General&path=Bluetooth Date& Time — prefs:root=General&path=DATE_AND_TIME FaceTime — prefs:root=FACETIME General— prefs:root=General Keyboard — prefs:root=General&path=Keyboard iCloud — prefs:root=CASTLE iCloud Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP International — prefs:root=General&path=INTERNATIONAL Location Services — prefs:root=LOCATION_SERVICES Music — prefs:root=MUSIC Music Equalizer — prefs:root=MUSIC&path=EQ Music VolumeLimit— prefs:root=MUSIC&path=VolumeLimit Network — prefs:root=General&path=Network Nike + iPod — prefs:root=NIKE_PLUS_IPOD Notes — prefs:root=NOTES Notification — prefs:root=NOTIFICATIONS_ID Phone — prefs:root=Phone Photos — prefs:root=Photos Profile — prefs:root=General&path=ManagedConfigurationList Reset — prefs:root=General&path=Reset Safari — prefs:root=Safari Siri — prefs:root=General&path=Assistant Sounds — prefs:root=Sounds SoftwareUpdate— prefs:root=General&path=SOFTWARE_UPDATE_LINK Store — prefs:root=STORE Twitter — prefs:root=TWITTER Usage — prefs:root=General&path=USAGE VPN — prefs:root=General&path=Network/VPN Wallpaper — prefs:root=Wallpaper Wi-Fi — prefs:root=WIFI Setting—prefs:root=INTERNET_TETHERING

    5、CollectionView相关:

    自定义layout的三个步骤(流水布局):
    一、准备数据,并保存到布局对象数组

    - (void)prepareLayout{ 
      [super prepareLayout]; 
      //准备item的数据  
      //定义数组用来保存每个item的frame 
      NSMutableArray* marr = [NSMutableArray array]; 
      //给每个item布局  
      for (int i = 0; i < self.clothesInfos.count; i ++) { 
      //创建布局对象  
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i     inSection:0];
        UICollectionViewLayoutAttributes* attr =     [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; 
        //取出数据  
        ClothesModel* model = self.clothesInfos[i]; 
        //计算frame 
        attr.frame = CGRectMake(itemX, itemY, itemWidth, itemHeight); //保存数据  [marr addObject:attr]; 
    }
       //计算footView的frame 
      NSIndexPath* footPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
      UICollectionViewLayoutAttributes* footAtt = [UICollectionViewLayoutAttributes   layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:footPath]; 
      footAtt.frame = CGRectMake(footX, footY, footW, footH); 
      [marr addObject:footAtt]; 
    // 把数据保存到布局数组    
      self.layoutAttributeMarr = marr;
    }
    

    二、计算内容区域

    - (CGSize)collectionViewContentSize{ 
      CGFloat CVCWidth = [UIScreen  mainScreen].bounds.size.width;       UICollectionViewLayoutAttributes* lastAtt = self.layoutAttributeMarr.lastObject;
      CGFloat CVCHeight = CGRectGetMaxY(lastAtt.frame)+_margin;
    //根据最大的Y值得到布局内容的高度  
      CGSize CVCSize = CGSizeMake(CVCWidth, CVCHeight); return CVCSize;
    }
    

    三、返回布局对象数组

    - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{ 
        return self.layoutAttributeMarr;
    }
    

    相关文章

      网友评论

          本文标题:ios开发一些小技巧(持续更新)

          本文链接:https://www.haomeiwen.com/subject/bqnxsttx.html