1
我有一個selectedShape
,一個方形UIView
,我正在通過移動其中一個corners
,UIImageView
來更改其大小。 corners
被放置在selectedShape
的每個角落,在selectedShape
的每個邊緣的中間,並且在selectedShape
的中心。UIImageView沒有隱藏?
雖然我通過移動corner
更改了selectedShape
的大小,但我試圖隱藏未被移動的其他corners
。但是,這不會發生爲you can see here。
class DrawViewController: UIViewController {
var corners: [UIImageView] = [UIImageView]()
override func viewDidLoad() {
super.viewDidLoad()
//set up corners: set color, add pan feature, count = 9
for i in 0...8 {
let corner = UIImageView(frame: CGRectMake(0, 0, 10, 10))
corner.backgroundColor = fontColor
corners.append(corner)
corner.tag = i
corner.userInteractionEnabled = true
let pan = UIPanGestureRecognizer(target: self, action: "moveShape:")
corner.addGestureRecognizer(pan)
}
func tapShape(sender: UITapGestureRecognizer){
selectedShape = sender.view!
//place each corner in view
for corner in corners {
self.view.insertSubview(corner, aboveSubview: selectedShape!)
}
//place each corner in proper place
corners = placeCorners(corners, selectedShape: selectedShape!)
}
func placeCorners(corners: [UIImageView], selectedShape: UIView)-> [UIImageView]{
//place the diagonal corners
corners[0].center.x = selectedShape.frame.minX
corners[0].center.y = selectedShape.frame.minY
corners[2].center.x = selectedShape.frame.maxX
corners[2].center.y = selectedShape.frame.minY
corners[6].center.x = selectedShape.frame.minX
corners[6].center.y = selectedShape.frame.maxY
corners[4].center.x = selectedShape.frame.maxX
corners[4].center.y = selectedShape.frame.maxY
//place the horizontal corners
corners[1].center.x = selectedShape.frame.minX
corners[1].center.y = selectedShape.frame.midY
corners[3].center.x = selectedShape.frame.maxX
corners[3].center.y = selectedShape.frame.midY
//place the vertical corners
corners[5].center.x = selectedShape.frame.midX
corners[5].center.y = selectedShape.frame.minY
corners[7].center.x = selectedShape.frame.midX
corners[7].center.y = selectedShape.frame.maxY
//place the center corner
corners[8].center = selectedShape.center
return corners
}
func moveShape(sender: UIPanGestureRecognizer){
let corner = sender.view as! UIImageView
//resize shape
let trans = sender.translationInView(canvas)
corner.center = CGPoint(x: corner.center.x + trans.x, y: corner.center.y + trans.y)
if let shape = selectedShape{
shape.frame.size = CGSize(width: shape.frame.width + trans.x , height: shape.frame.height + trans.y)
}
sender.setTranslation(CGPointZero, inView: canvas)
//hide corners while changing size
if (sender.state == UIGestureRecognizerState.Began || sender.state == UIGestureRecognizerState.Changed){
for c in corners{
//set panned corner to panIcon
if c.tag == corner.tag{
corner.backgroundColor = UIColor.clearColor()
corner.image = UIImage(named: "panIcon")
corner.contentMode = UIViewContentMode.ScaleAspectFit
}
c.hidden == true
}
}
//unhide corners when finish changing size
if(sender.state == UIGestureRecognizerState.Ended || sender.state == UIGestureRecognizerState.Failed || sender.state == UIGestureRecognizerState.Cancelled){
for c in corners{
//set panned corner back to grey square
if c.tag == corner.tag{
corner.backgroundColor = fontColor
corner.image = nil
}
c.hidden == false
}
if let shape = selectedShape{
corners = placeCorners(corners, selectedShape: shape)
}
}
}
}
我不知道這是怎麼回事,但我有一種感覺,這與參考文獻做,因爲當我在//set panned corner to panIcon
下的部分代碼更改corner
到c
...
//set panned corner to panIcon
if c.tag == corner.tag{
c.backgroundColor = UIColor.clearColor()
c.image = UIImage(named: "panIcon")
c.contentMode = UIViewContentMode.ScaleAspectFit
}
...正在移動的corner
不再在移動時更改其圖標。
任何有關如何解決這個問題的幫助將不勝感激!
您需要使用賦值運算符'='而不是'=='。 'c.hidden = true'和'c.hidden = false' – Caleb
ohhhh正確的感謝@Caleb指出這一點! – 14wml