最近在玩韩服,看到首页的一块动画蛮有意思的,仿写一下,O(∩_∩)O哈哈~
基本功能已经实现,还有些细节,等有空的时候补充 0.0
注释也后补,不是专业UI,图截的有点挫~~~
.h文件
@interfaceWsView :UIView
@property(nonatomic,assign)NSIntegerbtnTag;
@end
.m文件
#define WIDTHDiv[UIScreen mainScreen ].bounds.size.width
#define HEIGHTDiv[ UIScreen mainScreen ].bounds.size.height
#import"WsView.h"
@interfaceWsView()
@property(nonatomic,assign)BOOLleftHid;
@property(nonatomic,assign)BOOLrightHid;
@end
@implementationWsView
- (instancetype)initWithFrame:(CGRect)frame
{
self= [superinitWithFrame:frame];
if(self) {
self.btnTag=6;
self.leftHid=NO;
self.rightHid=NO;
for(inti =0; i <9; i++) {
UIButton*img = [UIButtonnew];
img.clipsToBounds=YES;
img.tag= i+100;
[imgsetImage:[UIImageimageNamed:[NSStringstringWithFormat:@"LOLIMage_%d_02.jpg",i]]forState:UIControlStateNormal];
img.imageView.contentMode=UIViewContentModeScaleAspectFill;
[imgaddTarget:selfaction:@selector(imgBtnClick:)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:img];
}
for(inti =0; i <2; i++) {
UIButton*btn = [UIButtonnew];
btn.tag= i+1000;
[btnaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[btnsetImage:[UIImageimageNamed:i==1?@"left":@"right"]forState:UIControlStateNormal];
[selfaddSubview:btn];
}
}
returnself;
}
-(void)btnClick:(UIButton*)senter{
if(senter.tag!=1000) {
self.btnTag--;
if(self.btnTag==0) {
self.leftHid=YES;
}else{
self.rightHid=NO;
}
}else{
self.btnTag++;
if(self.btnTag==8) {
self.rightHid=YES;
}else{
self.leftHid=NO;
}
}
[selfsetNeedsLayout];
}
-(void)imgBtnClick:(UIButton*)senter{
if(senter.tag==100) {
self.leftHid=YES;
self.rightHid=NO;
}elseif(senter.tag==108) {
self.rightHid=YES;
self.leftHid=NO;
}else{
self.rightHid=NO;
self.leftHid=NO;
}
self.btnTag= senter.tag-100;
[selfsetNeedsLayout];
}
-(void)layoutSubviews{
[superlayoutSubviews];
for(inti =0; i <2; i++) {
UIButton*btn = [selfviewWithTag:i+1000];
btn.frame=CGRectMake(WIDTHDiv-90-(i*50),55,40,40);
if(i !=0) {
if(self.leftHid) {
CGRectrect = btn.frame;
rect.size.width=0;
rect.size.height=0;
CGPointpoint = btn.center;
[UIViewanimateWithDuration:.3animations:^{
btn.frame= rect;
btn.center= point;
}];
}else{
CGRectrect = btn.frame;
rect.size.width=40;
rect.size.height=40;
CGPointpoint = btn.center;
[UIViewanimateWithDuration:.3animations:^{
btn.frame= rect;
btn.center= point;
}];
}
}else{
if(self.rightHid) {
CGRectrect = btn.frame;
rect.size.width=0;
rect.size.height=0;
CGPointpoint = btn.center;
[UIViewanimateWithDuration:.3animations:^{
btn.frame= rect;
btn.center= point;
}];
}else{
CGRectrect = btn.frame;
rect.size.width=40;
rect.size.height=40;
CGPointpoint = btn.center;
[UIViewanimateWithDuration:.3animations:^{
btn.frame= rect;
btn.center= point;
}];
}
}
}
for(inti =0; i <9; i++) {
UIButton*img = [selfviewWithTag:i+100];
if(i
[UIViewanimateWithDuration:.3animations:^{
img.frame=CGRectMake(i*66,100,66,HEIGHTDiv*.5);
}];
}
if(i ==self.btnTag) {
[UIViewanimateWithDuration:.3animations:^{
img.frame=CGRectMake(i*66,100,WIDTHDiv-8*66,HEIGHTDiv*.5);
}];
}
if(i >self.btnTag) {
[UIViewanimateWithDuration:.3animations:^{
img.frame=CGRectMake(i*66+(WIDTHDiv-9*66),100,66,HEIGHTDiv*.5);
}];
}
}
}
@end