动态实现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[][];
}; typedef struct XLMatrix XLMatrix; static XLMatrix XLMatrixMake(NSInteger column, NSInteger row) {
XLMatrix matrix;
matrix.column = column;
matrix.row = row;
for(NSInteger i = ; i < column; i++){
for(NSInteger j = ; j < row; j++){
matrix.matrix[i][j] = ;
}
} return matrix;
} static XLMatrix XLMatrixMakeFromArray(NSInteger column, NSInteger row, CGFloat *data) {
XLMatrix matrix = XLMatrixMake(column, row);
for (int i = ; i < column; i ++) {
CGFloat *t = data + (i * row);
for (int j = ; 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 = ; i < a.column; i ++){
for(NSInteger j = ; j < b.row; j ++){
for(NSInteger k = ; 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 == ) {
return point;
} CGFloat temp2[][] = {point.x, point.y, point.z, };
// XLMatrix pointM = XLMatrixMakeFromArray(1, 4, *temp2); XLMatrix result = XLMatrixMakeFromArray(, , *temp2); if (direction.z * direction.z + direction.y * direction.y != ) {
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[][] = {{, , , }, {, cos1, sin1, }, {, -sin1, cos1, }, {, , , }};
XLMatrix m1 = XLMatrixMakeFromArray(, , *t1);
result = XLMatrixMutiply(result, m1);
} if (direction.x * direction.x + direction.y * direction.y + direction.z * direction.z != ) {
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[][] = {{cos2, , -sin2, }, {, , , }, {sin2, , cos2, }, {, , , }};
XLMatrix m2 = XLMatrixMakeFromArray(, , *t2);
result = XLMatrixMutiply(result, m2);
} CGFloat cos3 = cos(angle);
CGFloat sin3 = sin(angle);
CGFloat t3[][] = {{cos3, sin3, , }, {-sin3, cos3, , }, {, , , }, {, , , }};
XLMatrix m3 = XLMatrixMakeFromArray(, , *t3);
result = XLMatrixMutiply(result, m3); if (direction.x * direction.x + direction.y * direction.y + direction.z * direction.z != ) {
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_[][] = {{cos2, , sin2, }, {, , , }, {-sin2, , cos2, }, {, , , }};
XLMatrix m2_ = XLMatrixMakeFromArray(, , *t2_);
result = XLMatrixMutiply(result, m2_);
} if (direction.z * direction.z + direction.y * direction.y != ) {
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_[][] = {{, , , }, {, cos1, -sin1, }, {, sin1, cos1, }, {, , , }};
XLMatrix m1_ = XLMatrixMakeFromArray(, , *t1_);
result = XLMatrixMutiply(result, m1_);
} XLPoint resultPoint = XLPointMake(result.matrix[][], result.matrix[][], result.matrix[][]); return resultPoint;
} #endif /* XLMatrix_h */
 //
// ViewController.m
// XLSphereView
//
// Created by 史晶晶 on 16/4/4.
// Copyright © 2016年 xiaolong. All rights reserved.
// #import "ViewController.h"
#import "XLSphereView.h" @interface ViewController () @property (nonatomic,strong) XLSphereView *sphereView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor];
CGFloat sphereViewW = self.view.frame.size.width - * ;
CGFloat sphereViewH = sphereViewW;
_sphereView = [[XLSphereView alloc] initWithFrame:CGRectMake(, , sphereViewW, sphereViewH)];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:];
for (NSInteger i = ; i < ; i ++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:[NSString stringWithFormat:@"晶%ld", i] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize: 7.0];
btn.backgroundColor = [UIColor colorWithRed:arc4random_uniform()/. green:arc4random_uniform()/. blue:arc4random_uniform()/. alpha:.];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:.];
btn.frame = CGRectMake(, , , );
btn.layer.cornerRadius = ;
btn.clipsToBounds = YES;
[btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[array addObject:btn];
[_sphereView addSubview:btn];
}
[_sphereView setItems:array];
[self.view addSubview:_sphereView]; } - (void)buttonPressed:(UIButton *)btn
{
[_sphereView timerStop]; [UIView animateWithDuration:0.3 animations:^{
btn.transform = CGAffineTransformMakeScale(., .);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 animations:^{
btn.transform = CGAffineTransformMakeScale(., .);
} completion:^(BOOL finished) {
[_sphereView timerStart];
}];
}];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

demo截图

3D标签-LMLPHP

下载地址:

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

05-28 21:01