黑马程序员技术交流社区
标题:
IOS常用方法总结
[打印本页]
作者:
18120184391
时间:
2015-10-9 21:46
标题:
IOS常用方法总结
1.某个点是否在某个frame里面
CGRectContainsPoint(button.frame, loc)
2.图片裁剪成小图片
//裁剪图片
- (UIImage *)imageClipWithImage:(UIImage *)image andindex:(int)index
{
CGFloat y = 0;
CGFloat w = 40;
CGFloat h = 47;
CGFloat x = index * w;
CGFloat scale = [UIScreen mainScreen].scale;
x *= scale;
y *= scale;
w *= scale;
h *= scale;
//剪切图片
CGImageRef cgImage = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(x, y, w, h));
UIImage *clipImage = [UIImage imageWithCGImage:cgImage];
return clipImage;
}
3.创建一个刷新的对象每秒60次
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(startRotation)];
[link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
4.执行玩核心动画执行
anim.delegate = self;
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
5.从storyboard中获取导航控制器
UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboard bundle:nil];
UINavigationController *nav = [sb instantiateInitialViewController];
6.从webView加载网页步骤
//获取webView
UIWebView *webView = (UIWebView *)self.view;
webView.delegate = self;
//创建url
// NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURL *url = [[NSBundle mainBundle] URLForResource:self.help.html withExtension:nil];
//创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//加载请求对象
[webView loadRequest:request];
7.网页加载完成后执行的jscode
//加载网页完毕执行的方法
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *jsCode = [NSString stringWithFormat:@"document.location.href = '#%@';",self.help.ID];
[webView stringByEvaluatingJavaScriptFromString:jsCode];
}
8.发短信、打电话、跳转到app各种操作
跳转到appAtore
- (void)jumpAppStore
{
NSString *path = @"https://www.baidu.com";
NSURL *url = [NSURL URLWithString:path];
UIApplication *app = [UIApplication sharedApplication];
[app openURL:url];
}
打电话
- (void)jumpAppStore
{
NSString *details = @“123456789”;
NSString *number = [NSString stringWithFormat:@"tel://%@",details];
NSURL *url = [NSURL URLWithString:number];
[[UIApplication sharedApplication] openURL:url];
}
发短信
- (void)jumpAppStore
{
NSString *number = [NSString stringWithFormat:@"sms://13837369891"];
NSURL *url = [NSURL URLWithString:number];
[[UIApplication sharedApplication] openURL:url];
}
//发短信的另一种方式
- (void)jumpAppStore
{
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
vc.body = @"阿发的说法都是";
vc.recipients = @[@"123213211231",@"121241141212"];
[self presentViewController:vc animated:YES completion:nil];
}
9.图片的拉伸
//设置登录按钮的背景图片
UIImage *normal = [UIImage imageNamed:@"RedButton"];
//拉伸背景图片
normal = [normal stretchableImageWithLeftCapWidth:normal.size.width * 0.5 topCapHeight:normal.size.height * 0.5];
10.push控制器时隐藏底部的tabbar
//push控制器时隐藏底部的tabbar
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
viewController.hidesBottomBarWhenPushed = YES;
[super pushViewController:viewController animated:animated];
}
11.图片裁剪的步骤
1.添加图片
2.创建上下文(和图片大小一致)
3.获取上下文
4.绘制裁剪图形
5.把路径加载到上下文
6.执行裁剪
7.把图片绘制到上下文
8.获取裁剪的图片
9.关闭上下文
10.保存
//保存到相册
UIImageWriteToSavedPhotosAlbum(clipImage, nil, nil, nil);
// 保存到沙盒中的Doc中
NSString *docpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [docpath stringByAppendingPathComponent:@"icon.png"];
NSData *data = UIImagePNGRepresentation(clipImage);
[data writeToFile:filePath atomically:YES];
11.截屏操作的步骤
// 创建上下文
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
// 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 将获取view的内容渲染上下文
[self.view.layer renderInContext:ctx];
// 获取image
UIImage *iconImage = UIGraphicsGetImageFromCurrentImageContext();
// 结束
UIGraphicsEndImageContext();
// 保存
UIImageWriteToSavedPhotosAlbum(iconImage, nil, nil, nil);
11.URL出现中文需要百分号转义
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
12.指纹识别
使用步骤:
{
1> 导入框架;
#import <LocalAuthentication/LocalAuthentication.h>
2> 指纹识别的实现:
{
1. 需要判断手机系统版本是否是 iOS 8.0 以上的版本.只有 iOS 8.0 以上才支持.
// 获得当前系统版本号
float version = [UIDevice currentDevice].systemVersion.floatValue;
if (version < 8.0 ) // 判断当前系统版本
{
NSLog(@"系统版本太低,请升级至最新系统");
return;
}
2. 实例化指纹识别对象,判断当前设备是否支持指纹识别功能(是否带有TouchID).
// 1> 实例化指纹识别对象
LAContext *laCtx = [[LAContext alloc] init];
// 2> 判断当前设备是否支持指纹识别功能.
if (![laCtx canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL])
{ // 如果设备不支持指纹识别功能
NSLog(@"该设备不支持指纹识别功能");
return;
};
3.指纹登陆(默认是异步方法)
// 指纹登陆
[laCtx evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"指纹登陆" reply:^(BOOL success, NSError *error)
{
// 如果成功,表示指纹输入正确.
if (success) {
NSLog(@"指纹识别成功!");
}else
{
NSLog(@"指纹识别错误,请再次尝试");
}
}];
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2