3D标签

动态实现3D标签,

主要代码:

//
//  XLMatrix.h
//  XLSphereView
//
//  Created by 史晶晶 on 16/4/4.
//  Copyright © 2016年 xiaolong. All rights reserved.
//

#ifndef XLMatrix_h
#define XLMatrix_h

#import "XLPoint.h"

struct XLMatrix {
    NSInteger column;
    NSInteger row;
    CGFloat matrix[4][4];
};

typedef struct XLMatrix XLMatrix;

static XLMatrix XLMatrixMake(NSInteger column, NSInteger row) {
    XLMatrix matrix;
    matrix.column = column;
    matrix.row = row;
    for(NSInteger i = 0; i < column; i++){
        for(NSInteger j = 0; j < row; j++){
            matrix.matrix[i][j] = 0;
        }
    }
    
    return matrix;
}

static XLMatrix XLMatrixMakeFromArray(NSInteger column, NSInteger row, CGFloat *data) {
    XLMatrix matrix = XLMatrixMake(column, row);
    for (int i = 0; i < column; i ++) {
        CGFloat *t = data + (i * row);
        for (int j = 0; j < row; j++) {
            matrix.matrix[i][j] = *(t + j);
        }
    }
    return matrix;
}

static XLMatrix XLMatrixMutiply(XLMatrix a, XLMatrix b) {
    XLMatrix result = XLMatrixMake(a.column, b.row);
    for(NSInteger i = 0; i < a.column; i ++){
        for(NSInteger j = 0; j < b.row; j ++){
            for(NSInteger k = 0; k < a.row; k++){
                result.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j];
            }
        }
    }
    return result;
}

static XLPoint XLPointMakeRotation(XLPoint point, XLPoint direction, CGFloat angle) {
    //    CGFloat temp1[4] = {direction.x, direction.y, direction.z, 1};
    //    XLMatrix directionM = XLMatrixMakeFromArray(1, 4, temp1);
    if (angle == 0) {
        return point;
    }
    
    CGFloat temp2[1][4] = {point.x, point.y, point.z, 1};
    //    XLMatrix pointM = XLMatrixMakeFromArray(1, 4, *temp2);
    
    XLMatrix result = XLMatrixMakeFromArray(1, 4, *temp2);
    
    if (direction.z * direction.z + direction.y * direction.y != 0) {
        CGFloat cos1 = direction.z / sqrt(direction.z * direction.z + direction.y * direction.y);
        CGFloat sin1 = direction.y / sqrt(direction.z * direction.z + direction.y * direction.y);
        CGFloat t1[4][4] = {{1, 0, 0, 0}, {0, cos1, sin1, 0}, {0, -sin1, cos1, 0}, {0, 0, 0, 1}};
        XLMatrix m1 = XLMatrixMakeFromArray(4, 4, *t1);
        result = XLMatrixMutiply(result, m1);
    }
    
    if (direction.x * direction.x + direction.y * direction.y + direction.z * direction.z != 0) {
        CGFloat cos2 = sqrt(direction.y * direction.y + direction.z * direction.z) / sqrt(direction.x * direction.x + direction.y * direction.y + direction.z * direction.z);
        CGFloat sin2 = -direction.x / sqrt(direction.x * direction.x + direction.y * direction.y + direction.z * direction.z);
        CGFloat t2[4][4] = {{cos2, 0, -sin2, 0}, {0, 1, 0, 0}, {sin2, 0, cos2, 0}, {0, 0, 0, 1}};
        XLMatrix m2 = XLMatrixMakeFromArray(4, 4, *t2);
        result = XLMatrixMutiply(result, m2);
    }
    
    CGFloat cos3 = cos(angle);
    CGFloat sin3 = sin(angle);
    CGFloat t3[4][4] = {{cos3, sin3, 0, 0}, {-sin3, cos3, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
    XLMatrix m3 = XLMatrixMakeFromArray(4, 4, *t3);
    result = XLMatrixMutiply(result, m3);
    
    if (direction.x * direction.x + direction.y * direction.y + direction.z * direction.z != 0) {
        CGFloat cos2 = sqrt(direction.y * direction.y + direction.z * direction.z) / sqrt(direction.x * direction.x + direction.y * direction.y + direction.z * direction.z);
        CGFloat sin2 = -direction.x / sqrt(direction.x * direction.x + direction.y * direction.y + direction.z * direction.z);
        CGFloat t2_[4][4] = {{cos2, 0, sin2, 0}, {0, 1, 0, 0}, {-sin2, 0, cos2, 0}, {0, 0, 0, 1}};
        XLMatrix m2_ = XLMatrixMakeFromArray(4, 4, *t2_);
        result = XLMatrixMutiply(result, m2_);
    }
    
    if (direction.z * direction.z + direction.y * direction.y != 0) {
        CGFloat cos1 = direction.z / sqrt(direction.z * direction.z + direction.y * direction.y);
        CGFloat sin1 = direction.y / sqrt(direction.z * direction.z + direction.y * direction.y);
        CGFloat t1_[4][4] = {{1, 0, 0, 0}, {0, cos1, -sin1, 0}, {0, sin1, cos1, 0}, {0, 0, 0, 1}};
        XLMatrix m1_ = XLMatrixMakeFromArray(4, 4, *t1_);
        result = XLMatrixMutiply(result, m1_);
    }
    
    XLPoint resultPoint = XLPointMake(result.matrix[0][0], result.matrix[0][1], result.matrix[0][2]);
    
    return resultPoint;
}


#endif /* XLMatrix_h */
 1 //
 2 //  ViewController.m
 3 //  XLSphereView
 4 //
 5 //  Created by 史晶晶 on 16/4/4.
 6 //  Copyright © 2016年 xiaolong. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "XLSphereView.h"
11 
12 @interface ViewController ()
13 
14 @property (nonatomic,strong) XLSphereView *sphereView;
15 
16 @end
17 
18 @implementation ViewController
19 
20 - (void)viewDidLoad {
21     [super viewDidLoad];
22     
23     self.view.backgroundColor = [UIColor blackColor];
24     CGFloat sphereViewW = self.view.frame.size.width - 30 * 2;
25     CGFloat sphereViewH = sphereViewW;
26     _sphereView = [[XLSphereView alloc] initWithFrame:CGRectMake(30, 100, sphereViewW, sphereViewH)];
27     NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
28     for (NSInteger i = 0; i < 40; i ++) {
29         UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
30         [btn setTitle:[NSString stringWithFormat:@"晶%ld", i] forState:UIControlStateNormal];
31         btn.titleLabel.font = [UIFont systemFontOfSize: 7.0];
32         btn.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255. green:arc4random_uniform(255)/255. blue:arc4random_uniform(255)/255. alpha:1.];
33         [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
34         btn.titleLabel.font = [UIFont systemFontOfSize:24.];
35         btn.frame = CGRectMake(0, 0, 60, 30);
36         btn.layer.cornerRadius = 3;
37         btn.clipsToBounds = YES;
38         [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
39         [array addObject:btn];
40         [_sphereView addSubview:btn];
41     }
42     [_sphereView setItems:array];
43     [self.view addSubview:_sphereView];
44     
45 }
46 
47 - (void)buttonPressed:(UIButton *)btn
48 {
49     [_sphereView timerStop];
50     
51     [UIView animateWithDuration:0.3 animations:^{
52         btn.transform = CGAffineTransformMakeScale(2., 2.);
53     } completion:^(BOOL finished) {
54         [UIView animateWithDuration:0.3 animations:^{
55             btn.transform = CGAffineTransformMakeScale(1., 1.);
56         } completion:^(BOOL finished) {
57             [_sphereView timerStart];
58         }];
59     }];
60 }
61 
62 - (void)didReceiveMemoryWarning {
63     [super didReceiveMemoryWarning];
64     // Dispose of any resources that can be recreated.
65 }
66 
67 @end

demo截图

下载地址:

https://github.com/henusjj/Label-effect

原文地址:https://www.cnblogs.com/henusyj-1314/p/5722776.html