iOS视图弹出、平移、旋转、翻转、剪切等变换效果实现
效果图:
1、定义属性
@property (nonatomic, strong) UIView *transformView;//发生变换的试图 @property (nonatomic, strong) UIView *backGroundView;//发生变换试图的父试图 @property (nonatomic, strong) UITableView *titleTableView; @property (nonatomic, strong) NSArray *dataSourceAry;//数据源数组
2、实现代码
-(void)poppingViewWithTransform:(NSString *)transformStr{ [self.view addSubview:self.backGroundView]; self.transformView.frame = CGRectMake(SCWidth/2-100, 200, 200, 200); if ([transformStr isEqualToString:@"缩放变换"]) { //很简单的变换效果,200,为Y轴从下往上平移,相反-200 是从上往下平移 self.transformView.transform = CGAffineTransformMakeScale(0.0, 0.0); [UIView animateWithDuration:0.5 animations:^{ self.transformView.transform = CGAffineTransformMakeScale(1.0, 1.0); }]; }else if([transformStr isEqualToString:@"平移变换"]){ //很简单的变换效果,200,为Y轴从下往上平移,相反-200 是从上往下平移,X轴变换同理 self.transformView.transform = CGAffineTransformMakeTranslation(0, -200); [UIView animateWithDuration:0.5 animations:^{ self.transformView.transform = CGAffineTransformMakeTranslation(0,0); }]; }else if([transformStr isEqualToString:@"翻转变换"]){ //下面注释的代码,最后Z轴改成一个数字,例12,可以实现旋转变换 //self.transformView.layer.transform = CATransform3DMakeRotation(12, 1, 1, 0); self.transformView.layer.transform = [self firstStep]; [UIView animateWithDuration:0.5 animations:^{ self.transformView.layer.transform = CATransform3DMakeRotation(0, 1, 0, 0); }]; }else if([transformStr isEqualToString:@"旋转变换"]){ //具体效果可以更改 M_PI_4 角度来实现 self.transformView.transform = CGAffineTransformMake(( cos(M_PI_4) ), ( sin(M_PI_4) ), -( sin(M_PI_4) ), (cos(M_PI_4) ), 0, 0); [UIView animateWithDuration:0.5 animations:^{ self.transformView.transform = CGAffineTransformMake(( cos(M_PI) ), ( sin(M_PI) ), -( sin(M_PI) ), (cos(M_PI) ), 0, 0); }]; }else if([transformStr isEqualToString:@"剪切变换"]){ //使用仿射基础方法CGAffineTransformMake,设置x和y都为0.5的斜切 //可以试着把第一个1换成0.5,看看效果 //可以试着把第二个1换成0.5,看看效果 self.transformView.transform = CGAffineTransformMake(1,0.5,0.5,1,0,0); [UIView animateWithDuration:0.5 animations:^{ self.transformView.transform = CGAffineTransformMake(1,0,0,1,0,0); }]; } self.transformView.layer.cornerRadius = 7; self.transformView.layer.masksToBounds = YES; [self.backGroundView addSubview:self.transformView]; 」
其中,翻转变化用到这个:
-(CATransform3D)firstStep{ //让transform1为单位矩阵 CATransform3D transform1 = CATransform3DIdentity; //z轴纵深的3D效果和CATransform3DRotate配合使用才能看出效果 //m34很重要 transform1.m34 = 1.0/-100; //x和y都缩小为原来的0.9,z不变 transform1 = CATransform3DScale(transform1, 0.9, 0.9, 1); //绕x轴向内旋转15度 transform1 = CATransform3DRotate(transform1,15.0f * M_PI/180.0f, 1, 0, 0); return transform1; }