您的位置:首页 > 移动开发 > IOS开发

iOS 开发技巧收集整理

2015-11-28 11:36 459 查看
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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS iPhone 开发技巧