iOS開發中會用到的一些方法

0.//解決手勢返回失效緩存

    self.interactivePopGestureRecognizer.delegate = self;ide

一、 發送短信動畫

 // 若是利用該方式發送短信, 當短信發送完畢或者取消以後不會返回應用程序this

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

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

        

        // 判斷當前設備可否發送短信代理

        if (![MFMessageComposeViewController canSendText]) {orm

            NSLog(@"當前設備不能發送短信");server

            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;

  }

相關文章
相關標籤/搜索