簡而言之,與在scrollView中使用平移手勢相關的問題。在UIScrollView中Swift的UIPanGesture
我有一個畫布(這是一個UIView的本身,而是個頭大)我在哪裏繪製超過他們每個人的啓用平移手勢(每個小的UIView對象我說的是,使用做一些UIView的對象另一個UIView類)。
現在畫布的高度和寬度可以更大......可以根據用戶輸入更改畫布。
所以爲了實現這個目的,我把canvas放在了UIScrollView中。現在畫布正在平穩增加或減少。
畫布上那些微小的UIView對象也可以旋轉。
現在的問題。
如果如果不滾動視圖裏面,然後在畫布內的每個UIView的對象是高檔運動,我不改變畫布大小(靜態),即,一切都工作得很好用下面的代碼。
如果畫布在UIScrollView中,那麼畫布可以向右滾動?現在在scrollview中,如果我平移畫布上的UIView對象,那麼這些小UIView對象不會跟隨手指的觸摸,而不是在觸摸在畫布上移動時在另一個點上移動。
N.B. - 不知怎的,我發現我需要在任何子視圖觸摸時禁用滾動視圖的滾動。爲此,我已經實現了NSNotificationCenter以將信號傳遞給父視圖控制器。
這是代碼。
此功能被父的viewController類內部定義
func canvusScrollDisable(){
print("Scrolling Off")
self.scrollViewForCanvus.scrollEnabled = false
}
func canvusScrollEnable(){
print("Scrolling On")
self.scrollViewForCanvus.scrollEnabled = true
}
override func viewDidLoad() {
super.viewDidLoad()
notificationUpdate.addObserver(self, selector: "canvusScrollEnable", name: "EnableScroll", object: nil)
notificationUpdate.addObserver(self, selector: "canvusScrollDisable", name: "DisableScroll", object: nil)
}
這是子視圖類畫布
import UIKit
class ViewClassForUIView: UIView {
let notification: NSNotificationCenter = NSNotificationCenter.defaultCenter()
var lastLocation: CGPoint = CGPointMake(0, 0)
var lastOrigin = CGPoint()
var myFrame = CGRect()
var location = CGPoint(x: 0, y: 0)
var degreeOfThisView = CGFloat()
override init(frame: CGRect){
super.init(frame: frame)
let panRecognizer = UIPanGestureRecognizer(target: self, action: "detectPan:")
self.backgroundColor = addTableUpperViewBtnColor
self.multipleTouchEnabled = false
self.exclusiveTouch = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func detectPan(recognizer: UIPanGestureRecognizer){
let translation = recognizer.translationInView(self.superview!)
self.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
switch(recognizer.state){
case .Began:
break
case .Changed:
break
case .Ended:
notification.postNotificationName("EnableScroll", object: nil)
default: break
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
notification.postNotificationName("DisableScroll", object: nil)
self.superview?.bringSubviewToFront(self)
lastLocation = self.center
lastOrigin = self.frame.origin
let radians:Double = atan2(Double(self.transform.b), Double(self.transform.a))
self.degreeOfThisView = CGFloat(radians) * (CGFloat(180)/CGFloat(M_PI))
if self.degreeOfThisView != 0.0{
self.transform = CGAffineTransformIdentity
self.lastOrigin = self.frame.origin
self.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
}
myFrame = self.frame
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
notification.postNotificationName("EnableScroll", object: nil)
}
}
現在滾動視圖是完全每當一個禁用其滾動UIView對象的觸摸是在scrollview內部的畫布上進行的,但有時候這些UIView對象是ar e沒有正確地跟隨畫布/屏幕上的觸摸位置。
我使用的是Swift 2.1和Xcode 7,但任何人都可以告訴我使用Objective-c/Swift的解決方案或丟失的東西嗎?
請嘗試[this](http://stackoverflow.com/a/23841224/2710486)? – zcui93
@ zcui93感謝隊友的幫助...讓我的嘗試,讓你知道.. :) – Tuhin