天天看點

iOS 開發技巧收集整理

1.使用NSTimer和CGAffineTransform實作最簡單的旋轉動畫

CGAffineTransform transform=CGAffineTransformMakeRotation(angle);
 view.transform = transform;
           

注:angle是浮點型參數,代表角度,表示距離原角度旋轉了多少

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _angle = 0;

    UIImageView* animationImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
    animationImage.frame=CGRectMake(100, 200, 100, 100);
    [self.view addSubview:animationImage];
    //初始化定時器NSTimer
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(startTimer:) userInfo:animationImage repeats:YES];
}

- (void)startTimer:(NSTimer *)tiemr {
    
    UIImageView *animationImage = timer.userInfo;
    _angle = _angle + 0.05;//angle角度 double angle;
    if (_angle > 6.28) {//大于 M_PI*2(360度) 角度再次從0開始
        _angle = 0;
    }
    CGAffineTransform transform=CGAffineTransformMakeRotation(_angle);
    animationImage.transform = transform;
}
           

2.給UILabel添加背景圖檔

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 300, 200)];
        label.text = @"背景圖檔如我";
        label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image"]];
        [self.view addSubview:label];
           

3.UIWebView加載播放Gif圖,可用作引導圖

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"1111" ofType:@"gif"];
     NSData *gif = [NSData dataWithContentsOfFile:filePath];
     UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.frame];
     [web loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
     web.userInteractionEnabled = NO;
     [self.view addSubview:web];
           

4.導航欄按鈕,最左或最右會有10個像素點左右是非響應區域,解決方法

UIButton  *leftButton= [UIButton buttonWithType:UIButtonTypeCustom];
     leftButton.frame = CGRectMake(0, 0, 24, 20);
     
     UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
     
     UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
     target:nil action:nil];
     negativeSpacer.width = -10;
     self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftItem];
           

5.監聽程式進入背景,前台

-(void)initNotification{
    //監聽是否重新進入程式程式.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(whenAppBecomActive)
                                                 name:UIApplicationDidBecomeActiveNotification object:nil];
    //監聽是否觸發home鍵挂起程式.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(whenAppResignActive)
                                                 name:UIApplicationWillResignActiveNotification object:nil];
}
-(void)whenAppBecomActive{
    NSLog(@"重新進入程式");
}
-(void)whenAppResignActive{
    NSLog(@"home鍵挂起程式,進入背景");
}
           

6.跳轉到蘋果商店

NSString *UrlString = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=1004900105" ];
        if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)){
               UrlString = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id1004900105"];
           }
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UrlString]];
           

7.給Label加删除線

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
    NSString *oldString = @"aaaaaaaaaaa";
    
    NSMutableAttributedString *attributString = [[NSMutableAttributedString alloc] initWithString:oldString];
    
    [attributString addAttribute:NSStrikethroughStyleAttributeName
                           value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
                           range:NSMakeRange(0, 11)];
    
    label.attributedText = attributString;
    
    label.textColor = [UIColor grayColor];
    
    [self.view addSubview:label];
           

8.本頁面隐藏導航欄PUSH到下一頁面,POP回來時導航欄出現黑色底部問題,解決方案

- (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:NO];
}