生活随笔
收集整理的這篇文章主要介紹了
IOS代码添加控件,控件移动,放大,缩小,旋转
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
控件移動,放大,縮小,旋轉
1,代碼添加控件
例如:
[objc]?view plaincopy
???? ? ? ? ? ? ? ?? ????? ????? ???UIButton?*btn1=[[UIButton?alloc]?init];??? btn1.frame=CGRectMake(20,?20,?100,?100);??? ???[btn1?setTitle:@"123"?forState:UIControlStateNormal];??? ???[btn1?setBackgroundImage:[UIImage?imageNamed:@"btn_01"]?forState:UIControlStateNormal];?? ???[btn1?setTitle:@"456"?forState:UIControlStateHighlighted];?? ???[btn1?setBackgroundImage:[UIImage?imageNamed:@"btn_02"?]?forState:UIControlStateHighlighted];?? ???[self.view?addSubview:btn1];??? [btn1?addTarget:self?action:@selector(clickMove:)?forControlEvents:UIControlEventTouchUpInside];???
2.控件的常見屬性
所有的UI控件最終都繼承自UIView:
UI控件的公共屬性都定義在UIView中
[objc]?view plaincopy
@property(nonatomic,readonly)?UIView?*superview;??? ?? @property(nonatomic,readonly,copy)?NSArray?*subviews;??? ?? @property(nonatomic)?NSInteger?tag;??? ?? @property(nonatomic)?CGAffineTransform?transform;??? ?? @property(nonatomic)?CGRect?frame;??? ?? @property(nonatomic)?CGRect?bounds;??? ?? @property(nonatomic)?CGPoint?center;???
3.UIView的常見方法
[objc]?view plaincopy
-?(void)addSubview:(UIView?*)view;??? ?? -?(void)removeFromSuperview;??? ?? -?(UIView?*)viewWithTag:(NSInteger)tag;??? ?? ?? ?? self.headBtn.frame.origin.y?-=?10;??
圖片移動的3中方式
1. transform方式來移動圖片 (最常用)
[objc]?view plaincopy
typedef?enum{?? ????kMovingDirTop,?? ????kMovingDirBottom,?? ????KMovingDirLieft,?? ????kMovingDirRight,?? kMovingDir;?? ?? ?????? ????switch?(btn.tag)?{?? ????????case?kMovingDirTop:?? ?????????????? ????????????self.headImageView.transform?=?CGAffineTransformTranslate(self.headImageView.transform,?0,?-20);?? ????????????break;?? ????????case?kMovingDirBottom:?? ????????????self.headImageView.transform?=?CGAffineTransformTranslate(self.headImageView.transform,?0,?20);?? ????????????break;?? ????????case?KMovingDirLieft:?? ????????????self.headImageView.transform?=?CGAffineTransformTranslate(self.headImageView.transform,?-20,?0);?? ????????????break;?? ????????case?kMovingDirRight:?? ????????????self.headImageView.transform?=?CGAffineTransformTranslate(self.headImageView.transform,?20,?0);?? ????????????break;?? ?????????????? ????????default:?? ????????????break;?? ????};??
2. 使用center來移動控件
[objc]?view plaincopy
?? CGPoint?p?=?self.headImageView.center;?? ?? switch?(btn.tag)?{?? ????case?kMovingDirTop:?? ?????????? ????????p.y?-=?20;?? ????????break;?? ????case?kMovingDirBottom:?? ????????p.y?+=?20;?? ????????break;?? ????case?KMovingDirLieft:?? ????????p.x?-=?20;?? ????????break;?? ????case?kMovingDirRight:?? ????????p.x?+=?20;?? ????????break;?? ?? ????default:?? ????????break;?? };?? ?? self.headImageView.center?=?p;??
3.使用frame來控制控件位置
[objc]?view plaincopy
??? ?? ?????CGRect?rect=self.headImageView.frame;?? ?switch?(btn.tag)?{?? ?????case?kMovingDirTop:?? ??????????? ?????????rect.origin.y?=?rect.origin.y-20;?? ?????????break;?? ?????case?kMovingDirBottom:?? ???????????rect.origin.y?=?rect.origin.y+20;?? ?????????break;?? ?????case?KMovingDirLieft:?? ???????????rect.origin.x?=?rect.origin.x-20;?? ????????break;?? ?????case?kMovingDirRight:?? ??????????rect.origin.x?=?rect.origin.x+20;?? ?????????break;?? ?? ?????default:?? ?????????break;?? };?? ?? elf.headImageView.frame=rect;??
放大,縮小,旋轉控件(3種方式)
[objc]?view plaincopy
typedef?enum{?? ????kAdd=5,?? ????KRemore,?? ????kRotateLeft,?? ????kRotatelRight,?? }kTranform;??
?1.transform方式,放大,縮小,旋轉控件 (最常用)
[objc]?view plaincopy
?? switch?(btn.tag)?{?? ????case?kAdd:?? ?????????? ????????self.headImageView.transform?=?CGAffineTransformScale(self.headImageView.transform,?1.2,?1.2);?? ????????break;?? ????case?KRemore:?? ????????self.headImageView.transform?=?CGAffineTransformScale(self.headImageView.transform,?0.8,?0.8);?? ????????break;?? ????case?kRotateLeft:?? ?????????? ?????????? ?????????? ????????self.headImageView.transform?=?CGAffineTransformRotate(self.headImageView.transform,?-M_PI_4);?? ????????break;?? ????case?kRotatelRight:?? ????????self.headImageView.transform?=?CGAffineTransformRotate(self.headImageView.transform,?M_PI_4);?? ????????break;?? ?????????? ????default:?? ????????break;?? };??
2. ?bounds方式,放大,縮小
[objc]?view plaincopy
?? ???CGRect?rect?=?self.headImageView.bounds;?? ?? ????switch?(btn.tag)?{?? ?? ????????case?kAdd:?? ????????????rect.size.width?+=?20;?? ????????????rect.size.height?+=?20;?? ????????????break;?? ????????case?KRemore:?? ????????????rect.size.width?-=?20;?? ????????????rect.size.height?-=?20;?? ????????????break;?? ?????????????? ????????default:?? ????????????break;?? ????};?? ?????? ????self.headImageView.bounds?=?rect;??
3.2. ?frame方式,放大,縮小
[objc]?view plaincopy
?? ????CGRect?rect?=?self.headImageView.frame;?? ?? ????switch?(btn.tag)?{?? ?? ????????case?kAdd:?? ????????????rect.size.width?+=?20;?? ????????????rect.size.height?+=?20;?? ????????????break;?? ????????case?KRemore:?? ????????????rect.size.width?-=?20;?? ????????????rect.size.height?-=?20;?? ????????????break;?? ?????????????? ????????default:?? ????????????break;?? ????};?? ?????? ????self.headImageView.frane?=?rect;??
總結:
通過以下屬性可以修改控件的位置
frame.origin
center
通過以下屬性可以修改控件的尺寸
frame.size
bounds.size
利用transform屬性可以修改控件的位移(位置)、縮放、旋轉
總結
以上是生活随笔為你收集整理的IOS代码添加控件,控件移动,放大,缩小,旋转的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。