Swift Demo

*1> 简单实现列表折叠效果

extension ViewController : UITableViewDelegate ,UITableViewDataSource{
    

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionView = UIView(frame: CGRect(x: 0, y: 0, width: screen_width, height: 44))
        sectionView.backgroundColor = UIColor.cyan
        sectionView.isUserInteractionEnabled = true
        let label = UILabel(frame: CGRect(x: 20, y: 0, width: 100, height: 44))
        label.text = dataArr[section]
        sectionView.addSubview(label)
        sectionView.tag = section
        let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.display1(tap:)))
        sectionView.addGestureRecognizer(tap)
        return sectionView
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let str = "\(section)"
        guard let value = dataDict[str] else {
            dataDict[str] = "0"
            return 0
        }
        if  (value as NSString).intValue == 1 {
            return 5
        }else{
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArr.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        cell?.textLabel?.text = "每一行"
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("\(indexPath.section)    \(indexPath.row)")
    }
}
extension ViewController{
    @objc fileprivate func display1(tap : UITapGestureRecognizer){

        let str = "\(tap.view!.tag)"
        
        let indexSet = NSIndexSet(index: Int((str as NSString).intValue))
        
        guard let value = dataDict[str] else {
            dataDict[str] = "1"
            tableView?.beginUpdates()
            tableView?.reloadSections(indexSet as IndexSet, with: .automatic)
            tableView?.endUpdates()
            return
        }
        if (value as NSString).intValue == 0 {
            dataDict[str] = "1"
        }else{
            dataDict[str] = "0"
        }
        tableView?.beginUpdates()
        tableView?.reloadSections(indexSet as IndexSet, with: .automatic)
        tableView?.endUpdates()
        
    }
}

* 2. 图片的放大缩小,移动

    // 捏合手势
    func didPinch(_ pinch: UIPinchGestureRecognizer) {
        if pinch.state != .changed {
            return
        }
        let scale = pinch.scale
        let location = pinch.location(in: view)
        let scaleTransform = imageV.transform.scaledBy(x: scale, y: scale)
        imageV.transform = scaleTransform
        
        let dx = imageV.frame.midX - location.x
        let dy = imageV.frame.midY - location.y
        let x = dx * scale - dx
        let y = dy * scale - dy
        
        // 这样的话计算会有错误
        //imageView.transform = imageView.transform.translatedBy(x: x, y: y);
        
        let translationTransform = CGAffineTransform(translationX: x, y: y)
        imageV.transform = imageV.transform.concatenating(translationTransform)
        
        pinch.scale = 1

    }
    // 利用Pan手势实现拖拽
    func didPan(_ pan: UIPanGestureRecognizer) {
        if pan.state != .changed {
            return
        }
        let scale = imageV.frame.size.width / UIScreen.main.bounds.size.width
        print(scale)
        let translation = pan.translation(in: view)
        let transform = imageV.transform.translatedBy(x: translation.x / scale, y: translation.y / scale)
        imageV.transform = transform
        pan.setTranslation(.zero, in: view)
        
    }

* 3. 购买详情动画

    func transformAnimation() {
        var transform = CATransform3DIdentity
        // 视图角度
        transform.m34 = -1.0 / 2000;
        // 尺寸缩小(transform对象,X轴,Y轴,Z轴)
        transform = CATransform3DScale(transform, 1, 1, 1)
        // 沿某轴旋转(transform对象,旋转角度,X轴,Y轴,Z轴)
        let angel = 15.0 * (CGFloat)(M_PI) / 180.0
        transform = CATransform3DRotate(transform, angel, 1, 0, 0)
        
        // 第二次 变形实在第一次的基础上
//        transform.m34 = transform.m34
        // 沿着某轴移动(transform对象,X轴,Y轴,Z轴)
        transform = CATransform3DTranslate(transform, 0, 10, 0)
        // 尺寸缩小(transform对象,X轴,Y轴,Z轴)
        transform = CATransform3DScale(transform, ( UIScreen.main.bounds.size.width-40)/UIScreen.main.bounds.size.width, ( UIScreen.main.bounds.size.height-20)/UIScreen.main.bounds.size.height, 1)
        self.view.layer.transform = transform
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,242评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,229评论 4 61
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,884评论 22 665
  • 我枕着你的臂弯睡着了 窗外有双眼睛死盯着我们 静寂的脸容有点吓人 看着那死寂的夜里有些恐慌 携手走在泥泞的道路上 ...
    田萍阅读 208评论 1 6
  • 8月底的时候,我参加了一个恋爱训练营,上了2天课程,花费学费不菲,我之前一直在想我参加的目的何在,直到周末讲座我才...
    明初的日记本阅读 281评论 0 0