1. 菜單按鈕的問題(UIMenuController)
通過新建UILabel類,對storyboard和Xib都可使用,但是要注意使用的前提
需要彈出此對象的位置和其自身的范圍大小,彈出的menu在哪個區(qū)域,使用方法:
setTarget : inView:
使menu可見,使用方法:
setMenuVisible: animated:
要使具體操作(copy, cut,deleted,paste)可以實現(xiàn),需要重寫以下方法:
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操作時,先把文本復(fù)制到粘貼板,下邊給出一些參考代碼:
//通過自定義menuItem,實現(xiàn)自定義文字
let copy : UIMenuItem = UIMenuItem(title: "復(fù)制", 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(){
//可進行相應(yīng)的行,或控件移除等操作
}
至于OC的寫法,這里不多說,相關(guān)連接參考
2.手勢沖突問題
網(wǎng)上多手勢識別大都是串行執(zhí)行,今天來說下并行執(zhí)行的,平移滑動同時兼顧縮放,由于swift中center不太好使,自己走了不少彎路,現(xiàn)將經(jīng)驗分享給大家:
直接代碼示例:
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()
})
}
}
/// 預(yù)覽相關(guān)參數(shù)
struct PreViewStruct {
/// 預(yù)覽開始的位置
static var beginTouchPoint:CGPoint!
/// 預(yù)覽的ImageView
static let imageView:UIImageView = {
let imageView = UIImageView(frame: CGRect.zero)
imageView.clipsToBounds = true
imageView.contentMode = .ScaleAspectFill
return imageView
}()
/// 預(yù)覽的背景試圖
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
}()
}