Swift 实用小技巧

    xiaoxiao2026-03-15  9

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><strong><span style="font-size:14px;">1.设置控件局部圆角</span></strong></span>

    <span style="white-space:pre"> </span><span style="font-size:18px;"><strong>//创建控件 let btn = UIButton.init(type: .Custom) btn.frame = CGRectMake(100, 100, 100, 60) btn.setTitle("圆角", forState: .Normal) btn.backgroundColor = UIColor.blueColor() self.view.addSubview(btn) //设置局部圆角 let rect = CGRectMake(0, 0, 100, 60)//发生形变的控件size let radio = CGSizeMake(10, 10) //圆角尺寸 let corner = UIRectCorner.BottomLeft.union(UIRectCorner.TopRight) //设置圆角的位置(左下角和右上角) let path = UIBezierPath.init(roundedRect: rect, byRoundingCorners: corner, cornerRadii: radio) let maskplayer = CAShapeLayer.init() maskplayer.frame = btn.bounds maskplayer.path = path.CGPath btn.layer.mask = maskplayer</strong></span>

    2.设置NavigationBar透明

    <pre name="code" class="plain"><span style="font-size:18px;">self.navigationController?.navigationBar.setBackgroundImage(UIImage.init(), </span><span style="font-size:18px;">forBarMetrics: .Default)</span> </pre><span style="font-size: 14px; "><strong>3.更换界面时NavigationBar显示和隐藏的过渡</strong></span></h4><h4 style="margin: 0px;"><span style="color: rgb(47, 47, 47); font-family: "lucida grande", "lucida sans unicode", lucida, helvetica, "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif; line-height: 1.8; font-size: 14px;">一个界面隐藏navigationBar,另一个显示,两个页面进行push和pop,或者是用侧滑手势返回时,不做处理就会造成返回时,navigationBar位置是空的,直接显示一个黑色视图或者下面一层的视图,很难看,此时就需要加入过渡的动画来隐藏或显示navigationBar:</span><div><pre name="code" class="plain" style="color: rgb(47, 47, 47); font-family: "lucida grande", "lucida sans unicode", lucida, helvetica, "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif; line-height: 1.8;"><span style="font-size:18px;"> override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) self.navigationController?.setNavigationBarHidden(false, animated: true) }</span>

    4.侧滑手势失效处理

    在自定义了返回按钮,或者某些webView,tableView等页面,侧滑手势会失效,可以在基类中设置如下代码: <span style="font-size:18px;">if self.navigationController?.respondsToSelector(#selector(interactivePopGestureRecognizer)) { //需要遵循一下手势的代理 self.navigationController.interactivePopGestureRecognizer.delegate = self; self.navigationController.interactivePopGestureRecognizer.enabled = YES; }</span>返回navigationController最顶层Controller的时候,再次侧滑,然后进行一个push界面的操作会发生卡顿,这是由于最顶层Controller手势依然有效,但是滑动后,并找不到返回的页面。造成卡顿,所以要在rootViewController中让此手势失效。把下面的设为NO <span style="font-size:18px;"> override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) self.navigationController?.interactivePopGestureRecognizer?.enabled = false }</span> 5. 获取图片的一部分 rect 是需要获取的图片尺寸,想取哪部分可以直接设置坐标及宽高 <span style="font-size: 14px;"> </span><span style="font-size:18px;"> let image = UIImage.init(named: "imgName") let imageRef = image?.CGImage let rect = CGRectMake(x, y, width, height) let imageRefRect = CGImageCreateWithImageInRect(imageRef, rect) let img = UIImage.init(CGImage: imageRefRect)</span>

    6.给TableView或者CollectionView的cell添加简单动画,在willDisplayCell方法中对将要显示的cell做动画

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *array = tableView.indexPathsForVisibleRows; NSIndexPath *firstIndexPath = array[0]; //设置anchorPoint cell.layer.anchorPoint = CGPointMake(0, 0.5); //为了防止cell视图移动,重新把cell放回原来的位置 cell.layer.position = CGPointMake(0, cell.layer.position.y); //设置cell 按照z轴旋转90度,注意是弧度 if (firstIndexPath.row < indexPath.row) { cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1.0); }else{ cell.layer.transform = CATransform3DMakeRotation(- M_PI_2, 0, 0, 1.0); } cell.alpha = 0.0; [UIView animateWithDuration:1 animations:^{ cell.layer.transform = CATransform3DIdentity; cell.alpha = 1.0; }]; } 注:整篇文章借鉴于  _南山忆   http://www.jianshu.com/p/a3156826c27c    保存下来以留后用

    转载请注明原文地址: https://ju.6miu.com/read-1307985.html
    最新回复(0)