0
次のコードは、メインビューの下に隠れているビューを、メインビュー上で完全に画面の下からドラッグできるようにするためのものです。何らかの理由で、以下のコードは絶対に何もしません。どんな提案も高く評価されます。ありがとう!アプリにドラッグアップメニューを追加するにはどうすればいいですか?
import UIKit
class ControlMenuView: FXBlurView {
var animator:UIDynamicAnimator!
var container:UICollisionBehavior!
var snap: UISnapBehavior!
var dynamicItem: UIDynamicItemBehavior!
var gravity:UIGravityBehavior!
var panGestureRecognizer: UIPanGestureRecognizer!
func setup() {
panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(ControlMenuView.handlePan(_:)))
panGestureRecognizer.cancelsTouchesInView = false
self.addGestureRecognizer(panGestureRecognizer)
animator = UIDynamicAnimator(referenceView: self.superview!)
dynamicItem = UIDynamicItemBehavior(items: [self])
dynamicItem.allowsRotation = false
dynamicItem.elasticity = 0
gravity = UIGravityBehavior(items: [self])
gravity.gravityDirection = CGVectorMake(0, -1)
container = UICollisionBehavior(items: [self])
configureContainer()
animator.addBehavior(gravity)
animator.addBehavior(dynamicItem)
animator.addBehavior(container)
}
func configureContainer(){
let boundaryWidth = UIScreen.mainScreen().bounds.size.width
container.addBoundaryWithIdentifier("upper", fromPoint: CGPointMake(0, self.frame.size.height), toPoint: CGPointMake(boundaryWidth, self.frame.size.height))
let boundaryHeight = UIScreen.mainScreen().bounds.size.height
container.addBoundaryWithIdentifier("lower", fromPoint: CGPointMake(0, boundaryHeight), toPoint: CGPointMake(boundaryWidth, boundaryHeight))
}
func handlePan (pan:UIPanGestureRecognizer){
let velocity = pan.velocityInView(self.superview).y
var movement = self.frame
movement.origin.x = 0
movement.origin.y = movement.origin.y + (velocity * 0.05)
if pan.state == .Ended {
panGestureEnded()
}else if pan.state == .Began {
snapToTop()
}else{
animator.removeBehavior(snap)
snap = UISnapBehavior(item: self, snapToPoint: CGPointMake(CGRectGetMidX(movement), CGRectGetMidY(movement)))
animator.addBehavior(snap)
}
}
func panGestureEnded() {
animator.removeBehavior(snap)
let velocity = dynamicItem.linearVelocityForItem(self)
if fabsf(Float(velocity.y)) > 250 {
if velocity.y < 0 {
snapToBottom()
}else{
snapToTop()
}
}else{
if let superViewHeigt = self.superview?.bounds.size.height {
if self.frame.origin.y > superViewHeigt/2 {
snapToTop()
}else{
snapToBottom()
}
}
}
}
func snapToBottom() {
gravity.gravityDirection = CGVectorMake(0, 2.5)
}
func snapToTop(){
gravity.gravityDirection = CGVectorMake(0, -2.5)
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.tintColor = UIColor.clearColor()
}
}
ビューは意図的にスクリーンの下に意図的に配置されていたので、それが問題の場合は問題ではありません。すぐに応答してくれてありがとう! –
私はそれを理解していますが、パンジェスチャ認識機能を「隠している」ビューに追加しています。私はジェスチャーレコグナイザーが最初にタッチを拾っていることにちょうど懐疑的です。 –