当前位置: 首页 > 科技观察

iOS半透明新手指南,教你制作

时间:2023-03-12 13:27:00 科技观察

1、效果展示这类新手引导比较常见,用来告诉用户某个按钮的作用,或者提醒用户可以进行某些交互操作。引导样式是在界面上添加一个半透明的引导图,高亮部分就是要高亮显示的区域。2.怎么做?方案有很多种,不同的方案有不同的优缺点。下面介绍两种常见的方案:1.方案一:生成整个导视图(一)。导出引导图让设计师导出每个尺寸的引导图,引导图只包含引导部分,不包含背景。导出导图样式如下:iPhone4、iPhone5、iPhone6、iPhone6plus需要导出4个尺寸。如果适用于iPad,还需要导出iPad(2)。编码是因为是整张图片导出的,所以代码部分简单,不用考虑布局问题,直接生成一个imageView放上去,然后给它添加点击事件代码如下:-(void)addGuideView{NSString*imageName=nil;if(DEVICE_IS_IPHONE5){imageName=@"guide-568h";}else{imageName=@"guide";}UIImage*image=[UIImageimageNamed:imageName];UIImageView*imageView=[[UIImageViewalloc]initWithImage:image];imageView.frame=self.view.bounds;imageView.userInteractionEnabled=YES;UITapGestureRecognizer*tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(dismissGuideView)];[imageViewaddGestureRecognizer:tap];[self.viewaddSubview:imageView];}这里需要特别注意:@3x的图片需要iOS8以上系统自动识别。如果要向前兼容,需要在图片名称中自行判断设备类型,然后指定对应的图片名称(3)。优缺点此解决方案的优点是.快只要设计师做好效果图,将遮罩层导出成各种规格,90%的工作量都在设计师身上,程序员只需要只需添加视图和事件b。维护成本低当界面发生变化或引导图需要调整时,只需设计师重新生成图片,然后更换即可。但是这个解决方案有很多缺点:a.图片占用了很多空间。每个设备一张图片。图片还是全屏,可能需要适配横屏,明显会增加app安装包的体积。b.图片不能重复使用。一张导览图只能在一个地方使用。不可能在其他地方使用它。2.方案二:图像拼接图像拼接的思路如下。将几张图片组合成一个遮罩层,然后在上面放置其他元素,如下图(1)所示。准备图片这里需要准备3张图片镂空椭圆遮罩层中间透明,四周白色。白色部分可以在遮蔽过程中变成任意颜色b.小箭头C.确定按钮#p#(2)。Coding这里只介绍部分代码编写过程a。接口定义:(void)showInView:(UIView)viewmaskBtn:(UIButton)btn;view:引导图的父视图btn:maskedbuttoninterface实现:(void)showInView:(UIView)viewmaskBtn:(UIButton)btn{self.parentView=view;self.maskBtn=btn;self.alpha=0;[viewaddSubview:self];[UIViewanimateWithDuration:0.2animations:^{self.alpha=1;}completion:nil];}b.修改遮罩层加载空心椭圆图片后,首先处理白色区域,改为黑色半透明UIImageimage=[UIImageimageNamed:@"whiteMask"];image=[imagemaskImage:[[UIColorblackColor]colorWithAlphaComponent:0.71]];UIImageViewimageView=[[UIImageViewalloc]initWithImage:image];(UIImage)maskImage:(UIColor)maskColor{CGRectrect=CGRectMake(0,0,self.size.width,self.size.height);界面图hicsBeginImageContext(rect.size);CGContextRefcontext=UIGraphicsGetCurrentContext();CGContextTranslateCTM(context,0,rect.size.height);CGContextScaleCTM(context,1.0,-1.0);CGContextClipToMask(context,rect,self.CGImage);CGContextSetFillColorWithColor(上下文,maskColor.CGColor);CGContextFillRect(context,rect);UIImage*smallImage=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();returnssmallImage;}这样会把图片的白色部分改成任意颜色,我们要改空心椭圆图片中的这样c.生成top,bottom,left,right4个黑色半透明view,分别放置在空心椭圆图片(UIView*)topMaskView{if(!_topMaskView){UIView*view=[[UIViewalloc]init];view.backgroundColor=[[UIColorblackColor]colorWithAlphaComponent:0.71];_topMaskView=view;}return_topMaskView;}(UIView*)bottomMaskView{if(!_bottomMaskView){UIView*view=[[UIViewalloc]init];view.backgroundColor=[[UIColorblackColor]colorWithAlphaComponent:0.71];_bottomMaskView=视图;}return_bottomMaskView;}(UIView*)leftMaskView{if(!_leftMaskView){UIView*view=[[UIViewalloc]init];view.backgroundColor=[[UIColorblackColor]colorWithAlphaComponent:0.71];_leftMaskView=view;}return_leftMaskView;}(UIView*)rightMaskView{if(!_rightMaskView){UIView*view=[[UIViewalloc]init];view.backgroundColor=[[UIColorblackColor]colorWithAlphaComponent:0.71];_rightMaskView=view;}return_rightMaskView;}d.Viewlayout这里有几点需要注意:c-1.拼接后的view的x,y,width,height的值需要取整,因为浮点数可能会导致连接部分出现白边,尤其是在iPhone6plus上,原因是手机的分辨率,因此这里最好使用整数是c-2.layoutSubviews函数中的布局。这里布局的好处是横竖屏都能平滑过渡,不需要手动更新布局代码如下:(void)layoutSubviews{[superlayoutSubviews];self.frame=_parentView.bounds;_maskBg。frame=self.bounds;_btnMaskView.center=[_maskBtn.superviewconvertPoint:_maskBtn.centertoView:_maskBtn.superview];CGRectbtnMaskRect=_btnMaskView.frame;btnMaskRect.size=CGSizeMake(地板(MaskRect.size.widtht(btnMaskRect.size.widtht),flosize.height));btnMaskRect.origin=CGPointMake(floor(btnMaskRect.origin.x),floor(btnMaskRect.origin.y));_btnMaskView.frame=btnMaskRect;_topMaskView.left=0;_topMaskView.top=0;_topMaskView.height=_btnMaskView.top;_topMaskView.width=self.width;_bottomMaskView.left=0;_bottomMaskView.top=_btnMaskView.bottom;_bottomMaskView。width=self.width;_bottomMaskView.height=self.height-_bottomMaskView.top;_leftMaskView.left=0;_leftMaskView.top=_btnMaskView.top;_leftMaskView.width=_btnMaskView.left;_leftMaskView.height=_btnMaskView.height;_rightMaskView。left=_btnMaskView.right;_rightMaskView.top=_btnMaskView.top;_rightMaskView.width=self.width-_rightMaskView.left;_rightMaskView.height=_btnMaskView.height;_arrwoView.right=_btnMaskView.left+24;_arrwoView.bottom=_btnMaskView.top-8;_tipsLabel.right=_arrwoView.left-6;_tipsLabel.bottom=_arrwoView.top+10;_okBtn.centerX=self.width/2;_okBtn.bottom=_btnMaskView.top-80;}(3)。缺点优点:a.节省空间,一般只需要几张小图,就可以组合成一张导览图b.图片可重复使用按钮、椭圆图片、小箭头等图片可能会被其他引导图片继续使用缺点:a.编码需要很长时间。每一个界面元素都需要通过编码实现,每一次变化也需要调整代码。适合长期更新的项目,节省app空间Demo地址:https://github.com/sunljz/demo/tree/master/GuideDemo【编者推荐】iOS开发基础知识:CoreAnimation(核心动画)iOS开发的一些技巧和窍门iOS的一些技巧developmentObscenity2iOS开发知识体系关于iOS多线程,看我够了【责任编辑:倪明TEL:(010)68476606】