首页 > 学院 > 开发设计 > 正文

IOS开发基础知识--碎片27

2019-11-14 18:03:06
字体:
来源:转载
供稿:网友

1:iOS中的round/ceil/floorf

extern float ceilf(float);extern double ceil(double);extern long double ceill(long double);extern float floorf(float);extern double floor(double);extern long double floorl(longdouble);extern float roundf(float);extern double round(double);extern long double roundl(longdouble);round:如果参数是小数,则求本身的四舍五入。ceil:如果参数是小数,则求最小的整数但不小于本身.floor:如果参数是小数,则求最大的整数但不大于本身. Example:如何值是3.4的话,则3.4 -- round 3.000000    -- ceil 4.000000    -- floor 3.00000

 

2:对数组进行转换,把原来二个值转化成一条的记录(满足左右排版布局)

NSMutableArray *mnewArray=[[NSMutableArray alloc]init];    NSArray *nameArray=@[@"1",@"2",@"3",@"4",@"5",@"6"];    int allCount=0;    if (nameArray.count%2==1) {        //说明是奇数        allCount=nameArray.count;    }    else    {        allCount=nameArray.count-1;    }    for (int i=0; i<allCount; i++) {        userModel *userM=[[userModel alloc]init];        userM.leftName=nameArray[i];        i++;        if (i!=nameArray.count) {            userM.rightName=nameArray[i];        }        [mnewArray addObject:userM];    }

 

3:APP拨打电话完又跳回到APP里,并监听它的状态

#import "ViewController.h"#import <CoreTelephony/CTCall.h>#import <CoreTelephony/CTCallCenter.h>@interface ViewController ()<UIAlertViewDelegate>@PRoperty(strong,nonatomic)UIWebView *phoneCallWebView;//电话监听@property (nonatomic, strong) CTCallCenter * center;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];}- (IBAction)sdfsdfsdfs:(id)sender {    [self directCall];        //监听电话    __weak typeof(self) weakSelf = self;    self.center = [[CTCallCenter alloc] init];    self.center.callEventHandler = ^(CTCall* call) {        if ([call.callState isEqualToString:CTCallStateDisconnected])        {            NSLog(@"Call has been disconnected");        }        else if ([call.callState isEqualToString:CTCallStateConnected])        {            NSLog(@"Call has just been connected");        }        else if([call.callState isEqualToString:CTCallStateIncoming])        {            NSLog(@"Call is incoming");        }        else if ([call.callState isEqualToString:CTCallStateDialing])        {            //监听再进入APP时弹出窗            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"                                                            message:@"message"                                                           delegate:weakSelf                                                  cancelButtonTitle:@"Cancel"                                                  otherButtonTitles:@"OtherBtn",nil];            [alert show];            NSLog(@"call is dialing");        }        else        {            NSLog(@"Nothing is done");        }    };}//打电话 结束完自动跳回APP-(void)directCall{    NSString *PhoneNum=@"10086";    NSURL *phoneURL=[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",PhoneNum]];    if (!self.phoneCallWebView) {        self.phoneCallWebView=[[UIWebView alloc]initWithFrame:CGRectZero];    }    [self.phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];}@end

注意:监听电话要引入CoreTelephony.framework,跳转回APP则是通过一个UIWebView实现

 

4:UIView的layoutSubviews和drawRect方法何时调用

首先两个方法都是异步执行。layoutSubviews方便数据计算,drawRect方便视图重绘。

layoutSubviews在以下情况下会被调用:

1、init初始化不会触发layoutSubviews。

2、addSubview会触发layoutSubviews。
3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化。
4、滚动一个UIScrollView会触发layoutSubviews。
5、旋转Screen会触发父UIView上的layoutSubviews事件。
6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件。
7、直接调用setLayoutSubviews。
 
drawRect在以下情况下会被调用:

1、如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。drawRect 掉用是在Controller->loadView, Controller->viewDidLoad 两方法之后掉用的.所以不用担心在 控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些View draw的时候需要用到某些变量 值).

2、该方法在调用sizeToFit后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。
3、通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。
4、直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0。
以上1,2推荐;而3,4不提倡
 
drawRect方法使用注意点:

 

1、 若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate 的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay 或 者 setNeedsDisplayInRect,让系统自动调该方法。
2、若使用calayer绘图,只能在drawInContext: 中(类似鱼drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法
3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来掉用setNeedsDisplay实时刷新屏幕
 
5:UIView中的坐标转换(convertPoint,convertRect)
// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;// 将rect从view中转换到当前视图中,返回在当前视图中的rect- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;例把UITableViewCell中的subview(btn)的frame转换到 controllerA中// controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button// 在controllerA中实现:CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];或CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];// 此rc为btn在controllerA中的rect或当已知btn时:CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];或CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表