思维锻炼(ios球体):如何根据一个数组成员是视图,有x,y,z属性的数量,将一个数组近似平铺到一个球形视图上?

1:x向右为正方向,y向下为正方向,z向屏幕内为正方向,球的面向你那一点z为单位1(z,图层显示有用),球心z为三分之二,跟球心对称,不面向你的那一点z为三分之一;数组元素为n个,因为球是对称的,所以递增为单位一/n*2=2/n;其次,每元素的对应穿过球心的z轴圆偏移角度p,应为不确定质数的平方根,且小于0.5,大于0,取(3-根号5)*pai好了。

 

思路:首先求y,将球半径看为单位一,则每个视图y值为2/n X i - 1;

对应的平面圆(求z和x有用)r半径为:根号下1-y的平方.

x为r乘以cos(p*(3-根号5)*pai),z为sin(p*(3-根号5)*pai),

好了,每一点的坐标都拿到了,就可以平铺圆了

 

下边上一个代码例子、效果图:

//
//  ViewController.m
//  CollectionViewtest
//
//

#import "ViewController.h"
#import "MyCell.h"
#import "MyFlowLayout.h"
#import "CollectionViewtest-Swift.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property(strong,nonatomic)UICollectionView *myCollectionView;
//球体
@property(assign,nonatomic)CGFloat velocity;
@property(assign,nonatomic)CGPoint last;
@property(strong,nonatomic)CADisplayLink *timer;
@property(strong,nonatomic)CADisplayLink *inertia;
@property(strong,nonatomic)NSMutableArray <UIButton*>*btns;
@property(strong,nonatomic)NSMutableArray <SpherePoint*>*coordinate;
@property(strong,nonatomic)SpherePoint *normalPoint;
@property(strong,nonatomic)NSMutableArray <UIButton*>*viewArr;
@property(strong,nonatomic)UIView *qiuBGV;;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    [self playView];
//    [self configCollectionView];
}
#pragma mark - 自定义folwlayout
-(void)configCollectionView{
    MyFlowLayout *flowLayout = [[MyFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(50, 50);
    flowLayout.minimumLineSpacing = 0;
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 70) collectionViewLayout:flowLayout];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    [self.myCollectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.backgroundColor = [UIColor greenColor];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 100;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    
    return cell;
}

#pragma mark - 球体布局
-(void)playView{
    UIView*view = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.width)];
    view.backgroundColor = [UIColor yellowColor];
    self.qiuBGV = view;
    [self.view addSubview:view];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWithGes:)];
    [view addGestureRecognizer:pan];
    
    self.viewArr = [[NSMutableArray alloc] initWithCapacity:0];
    self.coordinate = [NSMutableArray new];
    
    int sum = 50;
    CGFloat p1 = M_PI*(3-sqrt(5));
    CGFloat p2 = 2.0/sum;
    
    for (int i = 0; i<sum; i++) {
        UIButton *sView = [[UIButton alloc] initWithFrame:CGRectMake(view.frame.size.width/2.0, view.frame.size.height/2.0, 50, 50)];
        [sView setImage:[UIImage imageNamed:@"dog"] forState:UIControlStateNormal];
        
        
        CGFloat y = p2*i-1;
        CGFloat r = sqrt(1-y*y);
        CGFloat c = i*p1;
        CGFloat z = r*sin(c);
        CGFloat x = r*cos(c);
        
        sView.center = CGPointMake((x+1)*view.frame.size.width/2, (y+1)*view.frame.size.height/2);
        sView.layer.zPosition = z;
        sView.transform = CGAffineTransformScale(CGAffineTransformIdentity, (z+2)/3, (z+2)/3);
        
        SpherePoint *point = [[SpherePoint alloc] init:x :y :z];
        [self.coordinate addObject:point];
        
        [view addSubview:sView];
        [self.viewArr addObject:sView];
    }
    
    CGFloat a = (arc4random() % 10) - 5.0;
    CGFloat b= (arc4random() % 10) - 5.0;
    self.normalPoint = [[SpherePoint alloc] init:a :b :0];
    [self startAniamtion];
}
-(void)startAniamtion{
    self.timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(autoTurnRotation)];
    [self.timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void)autoTurnRotation{
    for (int i = 0; i<self.viewArr.count; i++) {
        [self updateSubviewsFrame:i direction:self.normalPoint angle:0.02];
    }
}
-(void)updateSubviewsFrame:(NSInteger)index direction:(SpherePoint*)direction angle:(CGFloat)angle {
    SpherePoint *point = self.coordinate[index];
    if (!point) {
        return;
    }
    SpherePoint *newPoint = [MatrixTool pointMakeRotationWithPoint:point direction:direction angle:angle];
    [self.coordinate replaceObjectAtIndex:index withObject:newPoint];
    [self setupSubview:newPoint index:index];
    
}
-(void)setupSubview:(SpherePoint*)point index:(NSInteger)index{
    UIButton* btn = self.viewArr[index];
    CGFloat x = (point.x + 1.0) * (self.qiuBGV.frame.size.width / 2.0);
    CGFloat y = (point.y + 1.0) * self.qiuBGV.frame.size.width / 2.0;
    btn.center = CGPointMake(x, y) ;
    
    CGFloat t = (point.z + 2.0) / 3.0;
    btn.transform = CGAffineTransformMakeScale(t, t);
    btn.layer.zPosition = t;
    btn.alpha = t;
    btn.userInteractionEnabled = (point.z>=0?YES:NO);
}
-(void)stopAniamtion{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}
-(void)inertiaStop{
    
    if (_inertia) {
        [_inertia invalidate];
        _inertia = nil;
    }
    [self startAniamtion];
}
-(void)panWithGes:(UIPanGestureRecognizer*)pan{
    if (pan.state == UIGestureRecognizerStateBegan) {
        self.last = [pan locationInView:self.qiuBGV];
        [self stopAniamtion];
        [self inertiaStop];
        
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        CGPoint current = [pan locationInView:self.qiuBGV];
        SpherePoint *point = [[SpherePoint alloc] init:_last.y-current.y :current.x-_last.x :0];
        
        CGFloat distance = sqrtf(point.x*point.x+point.y*point.y);
        CGFloat angle = distance / (self.qiuBGV.frame.size.width / 2.0);
        NSInteger count = self.viewArr.count;
        if (count > 0) {
            for (int i = 0; i<count; i++) {
                [self updateSubviewsFrame:i direction:point angle:angle];
            }
            
        }
        self.normalPoint = point;
        self.last = current;
        
    } else if (pan.state == UIGestureRecognizerStateEnded){
        
        CGPoint vector = [pan velocityInView:self.qiuBGV];
        
        self.velocity = sqrt(vector.x*vector.x+vector.y*vector.y);
        
        
        [self inertiaStart];
    }
}
-(void)inertiaStart{
    [self stopAniamtion];
    self.inertia = [CADisplayLink displayLinkWithTarget:self selector:@selector(inertiaStep)];
    [self.inertia addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void)inertiaStep{
    if (self.velocity<=0) {
        [self inertiaStop];
        return;
    }
    
    self.velocity = self.velocity - 70.0;
    CGFloat angle = self.velocity / self.qiuBGV.frame.size.width * 2.0 * (self.inertia.duration);
    for (int i = 0; i<self.viewArr.count; i++) {
        [self updateSubviewsFrame:i direction:self.normalPoint angle:angle];
    }
    
}
#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

球体旋转计算工具:

//
//  rotation.swift
//  CollectionViewtest
//
//

import Foundation
class Matrix: NSObject {
    var column: NSInteger
    var row: NSInteger
    var matrix: [[Float]]
    @objc init(_ c: NSInteger,
         _ r: NSInteger,
         _ mat: [[Float]] = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) {
        column = c
        row = r
        matrix = mat
    }
}

class SpherePoint: NSObject {
    @objc var x: Float = 0.0
    @objc var y: Float = 0.0
    @objc var z: Float = 0.0
    
   @objc init(_ x: Float, _ y: Float, _ z: Float) {
        self.x = x
        self.y = y
        self.z = z
    }
}

class MatrixTool: NSObject {
    
        
        @objc public static func pointMakeRotation(point: SpherePoint, direction dir: SpherePoint, angle: Float) -> SpherePoint {
            guard angle != 0 else {
                return point
            }
            
            let t: [[Float]] = [[point.x, point.y, point.z, 1]]
            var result: Matrix = Matrix(1, 4, t)
            
            if dir.z * dir.z + dir.y * dir.y != 0 {
                let cos1: Float = dir.z / sqrt(dir.z * dir.z + dir.y * dir.y)
                let sin1: Float = dir.y / sqrt(dir.z * dir.z + dir.y * dir.y)
                let t1 = [[1, 0, 0, 0], [0, cos1, sin1, 0], [0, -sin1, cos1, 0], [0, 0, 0, 1]]
                let m1 = Matrix(4, 4, t1)
                result = matrixMutiply(a: result, b: m1)
            }
            
            if dir.x * dir.x + dir.y * dir.y + dir.z * dir.z != 0 {
                let cos2: Float = sqrt(dir.y * dir.y + dir.z * dir.z) / sqrt(dir.x * dir.x + dir.y * dir.y + dir.z * dir.z);
                let sin2: Float = -dir.x / sqrt(dir.x * dir.x + dir.y * dir.y + dir.z * dir.z)
                let t2 = [[cos2, 0, -sin2, 0], [0, 1, 0, 0], [sin2, 0, cos2, 0], [0, 0, 0, 1]]
                let m2 = Matrix(4, 4, t2)
                result = matrixMutiply(a: result, b: m2)
            }
            
            let cos3 = cos(angle)
            let sin3 = sin(angle)
            let t3 = [[cos3, sin3, 0, 0], [-sin3, cos3, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
            let m3 = Matrix(4, 4, t3)
            result = matrixMutiply(a: result, b: m3)
            
            if dir.x * dir.x + dir.y * dir.y + dir.z * dir.z != 0 {
                let cos2 = sqrt(dir.y * dir.y + dir.z * dir.z) / sqrt(dir.x * dir.x + dir.y * dir.y + dir.z * dir.z)
                let sin2 = -dir.x / sqrt(dir.x * dir.x + dir.y * dir.y + dir.z * dir.z)
                let t2   = [[cos2, 0, sin2, 0], [0, 1, 0, 0], [-sin2, 0, cos2, 0], [0, 0, 0, 1]]
                let m2   = Matrix(4, 4, t2)
                result   = matrixMutiply(a: result, b: m2)
            }
            
            if dir.z * dir.z + dir.y * dir.y != 0 {
                let cos1 = dir.z / sqrt(dir.z * dir.z + dir.y * dir.y)
                let sin1 = dir.y / sqrt(dir.z * dir.z + dir.y * dir.y)
                let t1   = [[1, 0, 0, 0], [0, cos1, -sin1, 0], [0, sin1, cos1, 0], [0, 0, 0, 1]]
                let m1   = Matrix(4, 4, t1)
                result   = matrixMutiply(a: result, b: m1)
            }
            
            let resultPoint = SpherePoint(result.matrix[0][0], result.matrix[0][1], result.matrix[0][2])
            return resultPoint
        }
        
       @objc private static func matrixMutiply(a: Matrix, b: Matrix) -> Matrix {
            var matrix: Matrix = Matrix(a.column, b.row)
            for i in 0..<a.column {
                for j in 0..<b.row {
                    for k in 0..<a.row {
                        matrix.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j]
                    }
                }
            }
            return matrix
        }

}

 

 

更多问题,欢迎加群讨论:565191947

原文地址:https://blog.csdn.net/a787188834/article/details/80015795

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple 最新软件的错误和性能问题。
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只有5%的概率会遇到选择运营商界面且部分必须连接到iTunes才可以激活
一般在接外包的时候, 通常第三方需要安装你的app进行测试(这时候你的app肯定是还没传到app store之前)。
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应用变灰了。那么接下来我们看一下Flutter是如何实现的。Flutter中实现整个App变为灰色在Flutter中实现整个App变为灰色是非常简单的,只需要在最外层的控件上包裹ColorFiltered,用法如下:ColorFiltered(颜色过滤器)看名字就知道是增加颜色滤镜效果的,ColorFiltered( colorFilter:ColorFilter.mode(Colors.grey, BlendMode.
flutter升级/版本切换
(1)在C++11标准时,open函数的文件路径可以传char指针也可以传string指针,而在C++98标准,open函数的文件路径只能传char指针;(2)open函数的第二个参数是打开文件的模式,从函数定义可以看出,如果调用open函数时省略mode模式参数,则默认按照可读可写(ios_base:in | ios_base::out)的方式打开;(3)打开文件时的mode的模式是从内存的角度来定义的,比如:in表示可读,就是从文件读数据往内存读写;out表示可写,就是把内存数据写到文件中;
文章目录方法一:分别将图片和文字置灰UIImage转成灰度图UIColor转成灰度颜色方法二:给App整体添加灰色滤镜参考App页面置灰,本质是将彩色图像转换为灰度图像,本文提供两种方法实现,一种是App整体置灰,一种是单个页面置灰,可结合具体的业务场景使用。方法一:分别将图片和文字置灰一般情况下,App页面的颜色深度是24bit,也就是RGB各8bit;如果算上Alpha通道的话就是32bit,RGBA(或者ARGB)各8bit。灰度图像的颜色深度是8bit,这8bit表示的颜色不是彩色,而是256
领导让调研下黑(灰)白化实现方案,自己调研了两天,根据网上资料,做下记录只是学习过程中的记录,还是写作者牛逼
让学前端不再害怕英语单词(二),通过本文,可以对css,js和es6的单词进行了在逻辑上和联想上的记忆,让初学者更快的上手前端代码
用Python送你一颗跳动的爱心
在uni-app项目中实现人脸识别,既使用uni-app中的live-pusher开启摄像头,创建直播推流。通过快照截取和压缩图片,以base64格式发往后端。
商户APP调用微信提供的SDK调用微信支付模块,商户APP会跳转到微信中完成支付,支付完后跳回到商户APP内,最后展示支付结果。CSDN前端领域优质创作者,资深前端开发工程师,专注前端开发,在CSDN总结工作中遇到的问题或者问题解决方法以及对新技术的分享,欢迎咨询交流,共同学习。),验证通过打开选择支付方式弹窗页面,选择微信支付或者支付宝支付;4.可取消支付,放弃支付会返回会员页面,页面提示支付取消;2.判断支付方式,如果是1,则是微信支付方式。1.判断是否在微信内支付,需要在微信外支付。
Mac命令行修改ipa并重新签名打包
首先在 iOS 设备中打开开发者模式。位于:设置 - 隐私&安全 - 开发者模式(需重启)
一 现象导入MBProgressHUD显示信息时,出现如下异常现象Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_MBProgressHUD", referenced from: objc-class-ref in ViewController.old: symbol(s) not found for architecture x86_64clang: error: linker command failed wit
Profiles >> 加号添加 >> Distribution >> "App Store" >> 选择 2.1 创建的App ID >> 选择绑定 2.3 的发布证书(.cer)>> 输入描述文件名称 >> Generate 生成描述文件 >> Download。Certificates >> 加号添加 >> "App Store and Ad Hoc" >> “Choose File...” >> 选择上一步生成的证书请求文件 >> Continue >> Download。
今天有需求,要实现的功能大致如下:在安卓和ios端实现分享功能可以分享链接,图片,文字,视频,文件,等欢迎大佬多多来给萌新指正,欢迎大家来共同探讨。如果各位看官觉得文章有点点帮助,跪求各位给点个“一键三连”,谢啦~声明:本博文章若非特殊注明皆为原创原文链接。