天天看点

iOS 中用到 fabsf fabs abs函数的作用

if (sender.state==UIGestureRecognizerStateEnded) {
        endPoint =[sender translationInView:_webView];


        if ((endPoint.y - begainPoint.y)>fabs(endPoint.x - begainPoint.x)) {
             NSLog(@"====-----------------下滑动");
            [_webView reload];

        }else if ((begainPoint.y - endPoint.y ) > fabs(endPoint.x - begainPoint.x)){
            NSLog(@"====-----------------上滑动");
            DetialVController *DeVC = [[DetialVController alloc]init];
            DeVC.detailUrl = @"http://xxxxxx.aspx";



            //创建动画
            CATransition *animation = [CATransition animation];
            //设置运动轨迹的速度
            animation.timingFunction = UIViewAnimationCurveEaseInOut;
            //设置动画类型为立方体动画
            animation.type = @"rippleEffect";
            //设置动画时长
            animation.duration =f;
            //设置运动的方向
            animation.subtype =kCATransitionFromRight;
            //控制器间跳转动画
            [[UIApplication sharedApplication].keyWindow.layer addAnimation:animation forKey:nil];


            [self.navigationController pushViewController:DeVC animated:YES];

        }else if (fabs(endPoint.y - begainPoint.y ) < fabs(endPoint.x - begainPoint.x) && fabs(endPoint.x - begainPoint.x)> ){
            ListVController *listVC = [[ListVController alloc]init];
            listVC.listUrl = @"http:xxxxxxxx.aspx";


            //创建动画
            CATransition *animation = [CATransition animation];
            //设置运动轨迹的速度
            animation.timingFunction = UIViewAnimationCurveEaseInOut;
            //设置动画类型为立方体动画
            animation.type = @"cube";
            //设置动画时长
            animation.duration = f;
            //设置运动的方向
            animation.subtype =kCATransitionFromRight;
            //控制器间跳转动画
            [[UIApplication sharedApplication].keyWindow.layer addAnimation:animation forKey:nil];




            [self.navigationController pushViewController:listVC animated:YES];

            NSLog(@"====-----------------左右滑动");
        }
           

先解释一下上的代码的大概意思,就是对一个界面的的滑动手势做判断,下拉刷新,上拉跳转控制器,左右滑动跳转另外一个控制器。

为了简化代码用了一个手势从滑动方向判断,所以用到了绝对值比对判断首选左右滑动还是上下滑动。

//如果上下滑动距离大于左右首选左右
fabs(endPoint.y - begainPoint.y ) < fabs(endPoint.x - begainPoint.x)
           

PS: fabs、fabs、abs区别

以下三个函数都用来处理绝对值的。只不过处理的类型不一样。

int abs(int i);                  // 处理int类型的取绝对值

double fabs(double i); //处理double类型的取绝对值

float fabsf(float i);          /处理float类型的取绝对值
           

继续阅读