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];
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值