1. 菜单按钮的问题(UIMenuController)
通过新建UILabel类,对storyboard和Xib都可使用,但是要注意使用的前提
需要弹出此对象的位置和其自身的范围大小,弹出的menu在哪个区域,使用方法:
setTarget : inView:
使menu可见,使用方法:
setMenuVisible: animated:
要使具体操作(copy, cut,deleted,paste)可以实现,需要重写以下方法:
canPerformAction:(SEL)action withSender:(id)sender
e.g:
override func canPerformAction(action: Selector, withSender sender: AnyObject?)
-> Bool
{
if action == #selector(copyAction)
{
return true
}
else if action == #selector(deleteAction)
{
return true
}
return super.canPerformAction(action, withSender: sender)
}
当要进行copy,cut操作时,先把文本复制到粘贴板,下边给出一些参考代码:
//通过自定义menuItem,实现自定义文字
let copy : UIMenuItem = UIMenuItem(title: "复制", action:#selector(copyAction))
let delete : UIMenuItem = UIMenuItem(title: "删除", action: #selector(deleteAction))
let menu = UIMenuController.sharedMenuController()
menu.menuItems = [copy, delete]
menu.setTargetRect(某控件.frame, inView: 某控件.superview!)
menu.setMenuVisible(true, animated: true)
func copyAction(){
let board = UIPasteboard.generalPasteboard()
board.string = 某控件.text
}
func deleteAction(){
//可进行相应的行,或控件移除等操作
}
至于OC的写法,这里不多说,相关连接参考
2.手势冲突问题
网上多手势识别大都是串行执行,今天来说下并行执行的,平移滑动同时兼顾缩放,由于swift中center不太好使,自己走了不少弯路,现将经验分享给大家:
直接代码示例:
func pinchAC(sender:UIPinchGestureRecognizer){
let image = headerView.commentPhoto.image
if sender.state == UIGestureRecognizerState.Began {
var rect = self.view.convertRect(headerView.commentPhoto.frame, fromView: headerView)
rect.origin.y += 64
PreViewStruct.imageView.frame = rect
PreViewStruct.imageView.image = image
PreViewStruct.beginTouchPoint = sender.locationOfTouch(0, inView: self.view)
(UIApplication.sharedApplication().delegate as! AppDelegate).window?.addSubview(PreViewStruct.occlusionView)
(UIApplication.sharedApplication().delegate as! AppDelegate).window?.addSubview(PreViewStruct.imageView)
headerView!.commentPhoto.alpha = 0
}
if sender.state == UIGestureRecognizerState.Changed {
let currentPoint = sender.locationOfTouch(0, inView: self.view)
let scale = sender.scale < 1 ? 1 : sender.scale
PreViewStruct.occlusionView.backgroundColor = UIColor.blackColor()
PreViewStruct.occlusionView.alpha = (scale - 1) > 0.8 ? 0.8 : (scale - 1)
PreViewStruct.imageView.transform = CGAffineTransformMakeTranslation(currentPoint.x-PreViewStruct.beginTouchPoint.x, currentPoint.y-PreViewStruct.beginTouchPoint.y)
PreViewStruct.imageView.transform = CGAffineTransformScale(PreViewStruct.imageView.transform, scale, scale)
}
if sender.state == UIGestureRecognizerState.Ended {
weak var weakSelf = self
UIView.animateWithDuration(0.3, animations: {
PreViewStruct.occlusionView.backgroundColor = UIColor.blackColor()
PreViewStruct.occlusionView.alpha = 0
PreViewStruct.imageView.transform = CGAffineTransformIdentity
}, completion: { (Bool) in
weakSelf!.headerView!.commentPhoto.alpha = 1
PreViewStruct.occlusionView.removeFromSuperview()
PreViewStruct.imageView.removeFromSuperview()
})
}
}
/// 预览相关参数
struct PreViewStruct {
/// 预览开始的位置
static var beginTouchPoint:CGPoint!
/// 预览的ImageView
static let imageView:UIImageView = {
let imageView = UIImageView(frame: CGRect.zero)
imageView.clipsToBounds = true
imageView.contentMode = .ScaleAspectFill
return imageView
}()
/// 预览的背景试图
static let occlusionView:UIView = {
let imageView = UIView(frame: UIScreen.mainScreen().bounds)
return imageView
}()
/// 当前的 window
static let currentWindow:UIView? = {
return (UIApplication.sharedApplication().delegate as! AppDelegate).window
}()
}