ios开发函数(数学函数应用)

今天在计算collectionView存放最小间距的时候用到一函数 " ABS " 顺便就查了一下这些数学函数在代码中的使用..

 

 //-----------------------函数在代码中使用-----------------------------

    // 获得super已经计算好的布局属性

    NSArray *array = [super layoutAttributesForElementsInRect:rect];

  // 计算collectionView最中心点的x

    CGFloat centerX = proposedContentOffset.x + self.collectionView.frame.size.width * 0.5;

  // 存放最小的间距值

    CGFloat minDelta = MAXFLOAT;

    for (UICollectionViewLayoutAttributes *attrs in array) {

        if (ABS(minDelta) > ABS(attrs.center.x - centerX)) {

            minDelta = attrs.center.x - centerX;

        }

    }

//-------------------sin cos的应用代码-----------------

 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath

{

    NSInteger count = [self.collectionView numberOfItemsInSection:0];

    CGFloat radius = 70;

    CGFloat oX = self.collectionView.frame.size.width * 0.5;

    CGFloat oY = self.collectionView.frame.size.height * 0.5;

    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

    attrs.size = CGSizeMake(50, 50);

    if (count == 1) {

        attrs.center = CGPointMake(oX, oY);

    } else {

        CGFloat angle = (2 * M_PI / count) * indexPath.item;

        CGFloat centerX = oX + radius * sin(angle);

        CGFloat centerY = oY + radius * cos(angle);

        attrs.center = CGPointMake(centerX, centerY);

    }

    return attrs;

}

@end

//-------------------------------------------------------------------

 

rand() ----随机数

abs() / labs() ----整数绝对值

fabs() / fabsf() / fabsl() ----浮点数绝对值

floor() / floorf() / floorl() ----向下取整

ceil() / ceilf() / ceill() ----向上取整

round() / roundf() / roundl() ----四舍五入

sqrt() / sqrtf() / sqrtl() ----求平方根

fmax() / fmaxf() / fmaxl() ----求最大值

fmin() / fminf() / fminl() ----求最小值

hypot() / hypotf() / hypotl() ----求直角三角形斜边的长度

fmod() / fmodf() / fmodl() ----求两数整除后的余数

modf() / modff() / modfl() ----浮点数分解为整数和小数

frexp() / frexpf() / frexpl() ----浮点数分解尾数和二为底的指数

sin() / sinf() / sinl() ----求正弦值

sinh() / sinhf() / sinhl() ----求双曲正弦值

cos() / cosf() / cosl() ----求余弦值

cosh() / coshf() / coshl() ----求双曲余弦值

tan() / tanf() / tanl() ----求正切值

tanh() / tanhf() / tanhl() ----求双曲正切值

asin() / asinf() / asinl() ----求反正弦值

asinh() / asinhf() / asinhl() ----求反双曲正弦值

acos() / acosf() / acosl() ----求反余弦值

acosh() / acoshf() / acoshl() ----求反双曲余弦值

atan() / atanf() / atanl() ----求反正切值

atan2() / atan2f() / atan2l() ----求坐标值的反正切值

atanh() / atanhf() / atanhl() ----求反双曲正切值

posted @   super1250  阅读(314)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示