iOS开发中会用到的一些方法

0.//解决手势返回失效

    self.interactivePopGestureRecognizer.delegate = self;

1、 发送短信

 // 如果利用该方式发送短信, 当短信发送完毕或者取消之后不会返回应用程序

//        NSURL *url = [NSURL URLWithString:@"sms://10010"];

//        [[UIApplication sharedApplication] openURL:url];

        

        // 判断当前设备能否发送短信

        if (![MFMessageComposeViewController canSendText]) {

            NSLog(@"当前设备不能发送短信");

            return ;

        }

        

        MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];

        // 设置短信内容

        vc.body = @"吃饭了没?";

        // 设置收件人列表

        vc.recipients = @[@"10010"];

        // 设置代理

        vc.messageComposeDelegate = self;

        // 显示控制器

        [self presentViewController:vc animated:YES completion:nil];

 2. 打电话

         // 弊端:使用该方法进行拨号之后,当电话挂断之后不会返回应用程序, 会停留在通话记录界面

//        NSURL *url = [NSURL URLWithString:@"tel://13261936021"];

//        [[UIApplication sharedApplication] openURL:url];

        

        // 在拨打电话之后会提示用户是否拨打, 当电话挂断之后会返回应用程序

//        NSURL *url = [NSURL URLWithString:@"telprompt://13261936021"];

//        [[UIApplication sharedApplication] openURL:url];

 

4.  图片从中间拉伸

// 左端盖宽度

    NSInteger leftCapWidth = bgImage.size.width * 0.5f;

    // 顶端盖高度

    NSInteger topCapHeight = bgImage.size.height * 0.5f;

    

    bgImage = [bgImage stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];

 

5.弹出动画

//出厂动画

- (void)animationWith:(UIView *)view

{

    

    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];

    animation.duration = 0.5;

    

    NSMutableArray *values = [NSMutableArray array];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];

    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];

    animation.values = values;

    [view.layer addAnimation:animation forKey:nil];

    

    

}

6.处理键盘弹出事件, 调整搜索框的高度

  -(void)registNoti{

 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shown:) name:UIKeyboardWillShowNotification object:nil];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardWillHideNotification object:nil];

}

 

7.// 键盘显示

-(void) shown:(NSNotification*) notification

{

    // keyboardFrame

    CGRect initialFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    CGRect convertedFrame = [self.view convertRect:initialFrame fromView:nil];

    

    CGRect tvFrame = self.infoTabaleView.frame ;

    tvFrame.size.height = convertedFrame.origin.y - 60;

    self.infoTabaleView.frame = tvFrame ;

    

}

 

8.// 键盘隐藏

-(void) keyboardHidden:(NSNotification*) notification

{

    CGRect tvFrame = self.infoTabaleView.frame;

    tvFrame.size.height = _initialTVHeight;

    self.infoTabaleView.frame = tvFrame;

}

9.清理缓存

UIAlertView * alert =[ [UIAlertView alloc]initWithTitle:@"提示" message:@"是否删除缓存" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];

    

    NSString * documentPath =[NSString stringWithFormat:@"%@/Documents",NSHomeDirectory()];

    NSLog(@"%@",documentPath);

    BOOL isDataExist=[[NSFileManager defaultManager]fileExistsAtPath:documentPath];

    NSLog(@"~~~~~~~%d",isDataExist);

    if (isDataExist)

    {

        long long content=[[[NSFileManager defaultManager]attributesOfItemAtPath:documentPath error:nil]fileSize];

        float size=content/1024.0;

        NSString * content2 = [NSString stringWithFormat:@"%.2fMB",size];

        alert.message = [@"是否删除缓存" stringByAppendingString:content2];

        [ [NSFileManager defaultManager]removeItemAtPath:documentPath error:nil];

        [alert show] ;

    }

    else

    {

        alert.message = @"暂无缓存";

        [alert show];

    }

 

10.图片裁剪: 比如星级显示的时候:

 starImageView.contentMode=UIViewContentModeLeft;

    //设置剪裁

    starImageView.clipsToBounds=YES;

float x=65/5.0*num;

    starImageView.frame=CGRectMake(0, 0, x, 23);

 

11.判断版本号:

 NSString *key = @"CFBundleVersion";

    

    // 取出沙盒中存储的上次使用软件的版本号

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *lastVersion = [defaults stringForKey:key];

    

    // 获得当前软件的版本号

    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];

    

    if ([currentVersion isEqualToString:lastVersion]) {

        // 显示状态栏

      

    } else { // 新版本

        [defaults setObject:currentVersion forKey:key];

        [defaults synchronize];

    }

 

 

12.根据颜色得到图片

- (UIImage *)imageWithColor:(UIColor *)color

{

    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return image;

}

13.// 得当前View上的图片

+ (UIImage *)captureImageWithView:(UIView *)view

{

    // 1.创建bitmap上下文

    UIGraphicsBeginImageContext(view.frame.size);

    // 2.将要保存的view的layer绘制到bitmap上下文中

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    // 3.取出绘制号的图片

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    return newImage;

}

14.图片压缩

 + (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize

  {

  // Create a graphics image context

  UIGraphicsBeginImageContext(newSize);

  // Tell the old image to draw in this new context, with the desired

  // new size

  [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

  // Get the new image from the context

  UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

  // End the context

  UIGraphicsEndImageContext();

  // Return the new image.

  return newImage;

  }

posted @ 2016-06-23 14:37  凡晓晓  阅读(184)  评论(0编辑  收藏  举报