UIView常見屬性和方法
UIView(UIViewGeometry):幾何
frame:邊框,CGSize,CGPoint,自身左上角相對父容器的位置
bounds:邊界,x和y默認為0,修改后不會影響自身位置,通過它可以修改控件的大小
center:視圖的中心點,通過它可以修改控件的位置
transform:通過它可以修改位置,也可以修改大小,還可以做旋轉
UIView(UIViewHierarchy):層次
superview:父控件
subviews:所有子控件
addSubview:添加一個子控件
removeFromSuperview:從父控件中移除自己
UIView(UIViewRendering):渲染
backgroundColor:背景顏色
alpha:透明度
UIView(UIViewAnimation):動畫
UIView(UIViewAnimationWithBlocks):block動畫
transform
const CGAffineTransform CGAffineTransformIdentity:復原
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat tx,
CGFloat ty) 在初始狀態(tài)下移動
CGAffineTransform CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)在初始狀態(tài)下縮放
CGAffineTransform CGAffineTransformMakeRotation(CGFloat angle)在初始狀態(tài)下旋轉
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,
CGFloat tx, CGFloat ty) 在形變的狀態(tài)下移動
CGAffineTransform CGAffineTransformScale(CGAffineTransform t,
CGFloat sx, CGFloat sy) 在形變的狀態(tài)下縮放
CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,
CGFloat angle) 在形變的狀態(tài)下旋轉
通過frame修改大小,左上角保持不變
通過bounds修改大小,中心點保持不變
block動畫
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion
duration:持續(xù)時間
delay:延遲時間
dampingRatio:彈跳系數(shù)
velocity:動畫完成速度
options:選項
(void (^)(void))animations:要執(zhí)行動畫的代碼塊
(void (^ __nullable)(BOOL finished))completion:動畫完成執(zhí)行的代碼塊
幀動畫
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
}
-(void)setupUI{
NSMutableArray *arrM = [NSMutableArray array];
int count = 25;
for (int i = 1 ; i <= count; i++) {
NSString *name =[NSString stringWithFormat:@"father%03d",i];
UIImage *img = [UIImage imageNamed:name];
[arrM addObject:img];
}
self.imgView.image = [UIImage animatedImageWithImages:arrM duration:count * 0.08];
}
@end
咻一咻
v.layer.cornerRadius 設置視圖的圓角
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
}
-(void)setupUI{
//設置背景顏色
self.view.backgroundColor = [UIColor colorWithRed:26/255.0 green:28/255.0 blue:49/255.0 alpha:1.0];
//添加支付寶按鈕
UIButton *btn = [[UIButton alloc] init];
[btn setImage:[UIImage imageNamed:@"alipay_msp_op_success"] forState:UIControlStateNormal];
[btn sizeToFit];
btn.center = self.view.center;
[self.view addSubview:btn];
//設置背景circle
UIView *v = [self createCircle];
[self.view insertSubview:v belowSubview:btn];
//添加點擊事件
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
//點擊觸發(fā)
-(void)click:(UIButton *)sender{
//禁用交互
sender.enabled = NO;
//生成效果
for (int i = 0; i < 20; i++) {
UIView *v = [self createCircle];
[self.view insertSubview:v belowSubview:sender];
//縮放倍數(shù)
CGFloat scale = 8.0;
[UIView animateWithDuration:1.8
delay:0.5 *i
options:0
animations:^{
v.backgroundColor = self.view.backgroundColor;
v.transform = CGAffineTransformMakeScale(scale, scale);
} completion:^(BOOL finished) {
[v removeFromSuperview];
}];
}
}
//生成view
-(UIView *)createCircle{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
v.center = self.view.center;
v.layer.cornerRadius = 50;
v.backgroundColor = [UIColor colorWithRed:47/255.0 green:167/255.0 blue:244/255.0 alpha:1.0] ;
return v;
}
@end