废话不多说,其实也是相当于给UITextField,UITextView添加自定义的系统弹出菜单。
那第一当然是先添加一个UIMenuItem
let pasteboard = UIPasteboard.generalPasteboard()
if pasteboard.image != nil {
let menuController = UIMenuController.sharedMenuController()
let pasteMenuItem = UIMenuItem.init(title: "粘贴", action: "pasteImage")
menuController.menuItems = [pasteMenuItem]
menuController.menuVisible = true
}
然后在程序里加入下面这个方法,这个方法主要用来出来那些动作是被响应的,比如说:copy,cut,paste以及自定义的一些响应方法等等,有兴趣的可以在自己的工程里试试,返回YES表示响应对应的操作,返回NO则表示不响应:(其实这里不写也是行的)
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == "pasteImage" {
return true
}
return false
}
在然后就是显示图片咯
func pasteImage() {
let pasteboard = UIPasteboard.generalPasteboard()
self.pasteImageView = UIImageView.init(image: pasteboard.image)
UIApplication.sharedApplication().keyWindow?.addSubview(self.pasteImageView)
self.pasteImageView.center = self.view.center
self.pasteImageView.bounds = CGRectMake(0, 0, self.view.frame.size.width-30, (self.view.frame.size.width-30)*(pasteboard.image?.size.height)!/(pasteboard.image?.size.width)!)
self.pasteImageView.transform = CGAffineTransformMakeTranslation(0, self.view.frame.size.height)
UIView.animateWithDuration(1, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1, options: .CurveEaseInOut, animations: { () -> Void in
self.pasteImageView.transform = CGAffineTransformMakeTranslation(0, 0)
}, completion: nil)
self.view.endEditing(true)
}
效果图