【好书】项目学习笔记 4

1.BookTitleView的实现

依然是用纯代码实现界面

    var BookCover: UIButton?
    var BookNames: JVFloatLabeledTextField?
    var BookEditor: JVFloatLabeledTextField?

    var delegate: BookTitleDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.BookCover = UIButton(frame: CGRect(x: 10, y: 8, width: 110, height: 141))
        self.BookCover?.setImage(UIImage(named: "Cover.png"), for: .normal)
        self.addSubview(self.BookCover!)
        self.BookCover?.addTarget(self, action: #selector(BookTitleDelegate.choiceCover), for: .touchUpInside)
        
        self.BookNames = JVFloatLabeledTextField(frame: CGRect(x: 128, y: 8+40, width: SCREEN_WIDTH-128-5, height: 30))
        self.BookEditor = JVFloatLabeledTextField(frame: CGRect(x: 128, y: 8+40+70, width: SCREEN_WIDTH-128-5, height: 30))
        
        self.BookNames?.placeholder = "书名"
        self.BookEditor?.placeholder = "作者"
        
        self.BookNames?.floatingLabelFont = UIFont(name: MY_FONT, size: 17)
        self.BookEditor?.floatingLabelFont = UIFont(name: MY_FONT, size: 17)
        
        self.BookNames?.font = UIFont(name: MY_FONT, size: 17)
        self.BookEditor?.font = UIFont(name: MY_FONT, size: 17)
        
        self.addSubview(self.BookNames!)
        self.addSubview(self.BookEditor!)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not implemeted")
    }

2.实现点击封面后执行其他动作

这里用到了 protocol 即可选接口

@objc protocol BookTitleDelegate {
    @objc optional func choiceCover()
}

为什么要使用Objc的接口是因为,Objc的接口方法是可选实现的,但swift的方法必须实现

然后在 pushNewBookViewController 中继承 BookTitleDelegate ,然后实现 choiceCover 方法

   func choiceCover() {
        //print("ChoiceCover")
        let vc = PhotoPickerViewController()
        vc.delegate = self
        self.present(vc, animated: true, completion: nil)
    }

3.实现跳出选择相册或拍照

新建了一个 PhotoPickerViewController

    var alert: UIAlertController?
    var picker: UIImagePickerController?
    var delegate: PhotoPickerDelegate!
    
    init() {
        super.init(nibName: nil, bundle: nil)
        //使下一层背景透明
        self.modalPresentationStyle = .overFullScreen
        self.view.backgroundColor = UIColor.clear
        
        self.picker = UIImagePickerController()
        picker?.allowsEditing = false //因为默认编辑是正方形的,所以需要自己做截图
        self.picker?.delegate = self
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not implemeted")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        if (self.alert == nil) {
            self.alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
            self.alert?.addAction(UIAlertAction(title: "从相册中选择", style: .default, handler: { (action) -> Void in
                self.localPhoto()
            }))
            self.alert?.addAction(UIAlertAction(title: "打开相机", style: .default, handler: { (action) -> Void in
                self.takePhoto()
            }))
            self.alert?.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (action) -> Void in
                
            }))
            self.present(alert!, animated: true, completion: nil)
        }
    }
    
    //打开相机
    func takePhoto() {
        if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
            self.picker?.sourceType = .camera
            self.present(self.picker!, animated: true, completion: { 
                
            })
            
        }else {
            let alertView = UIAlertController(title: "相机不可用", message: nil, preferredStyle: .alert)
            alertView.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
            self.present(alertView, animated: true, completion: nil)
        }
    }
    
    //打开相册
    func localPhoto() {
        self.picker?.sourceType = .photoLibrary
        self.present(self.picker!, animated: true, completion: { () -> Void in
        
        })
    }
    
    //取消回调
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.picker?.dismiss(animated: true, completion: { () -> Void in
            self.dismiss(animated: true, completion: nil)
        })
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        self.picker?.dismiss(animated: true, completion: { () -> Void in
            self.dismiss(animated: true, completion: { () -> Void in
                self.delegate.getImgeFromPicker(image: image)
            })
        })
    }

4.实现更换封面

同样用到 protocol

protocol PhotoPickerDelegate {
    func getImgeFromPicker(image: UIImage)
}

在选取相片结束后

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        self.picker?.dismiss(animated: true, completion: { () -> Void in
            self.dismiss(animated: true, completion: { () -> Void in
                self.delegate.getImgeFromPicker(image: image)
            })
        })
    }

然后在 pushNewBookViewController 中继承 PhotoPickerDelegate

实现 getImgeFromPicker 方法

      func getImgeFromPicker(image: UIImage) {
        self.BookTitle?.BookCover?.setImage(image, for: .normal)
    }

即完成了书籍封面的更换。

Comments
Write a Comment