Simulator Screen Shot 2016年3月23日 上午11.02.35.png
DragSquaresView.swift
import UIKit
class DragSquaresView: UIView {
let itemArray = NSMutableArray()
var contain:Bool!
var startPoint: CGPoint!
var originPoint: CGPoint!
override init(frame: CGRect) {
super.init(frame: frame)
createdView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func createdView() {
let W = UIScreen.mainScreen().bounds.width //屏幕寬度
_ = UIScreen.mainScreen().bounds.height //屏幕高度
let allView = 40 // 全部視圖個(gè)數(shù)
let mainViewW:CGFloat = W/4 //視圖寬度
let mainViewH:CGFloat = W/4 //視圖高度
let totalloc = 4 //列數(shù)
let margin = (W - mainViewW * CGFloat(totalloc))/5 //邊緣
for i in 0..<allView {
let loc = i%totalloc //列數(shù)
let row = i/totalloc //行數(shù)(1/3=0,2/3=0,3/3=1)
let viewX = margin + (margin + mainViewW) * CGFloat(loc)
let viewY = margin + (margin + mainViewH) * CGFloat(row)
//創(chuàng)建視圖
let btn = UIButton(frame: CGRect(x: viewX, y: viewY+20, width: mainViewW, height: mainViewH))
btn.tag = i
btn.titleLabel?.font = UIFont.systemFontOfSize(20)
btn.backgroundColor = randonColor()
btn.setTitle("\(i+1)", forState: .Normal)
self.addSubview(btn)
let longGesture = UILongPressGestureRecognizer(target: self, action: "buttonLongPressed:")
btn.addGestureRecognizer(longGesture) //創(chuàng)建手勢(shì)
itemArray.addObject(btn)
}
}
// 手勢(shì)方法
func buttonLongPressed(sender: UILongPressGestureRecognizer) {
let btn = sender.view as! UIButton
switch sender.state {
case .Began:
startPoint = sender.locationInView(sender.view)
originPoint = btn.center
UIView.animateWithDuration(0.2, animations: { () -> Void in
btn.transform = CGAffineTransformMakeScale(1.1, 1.1)
btn.alpha = 0.7
})
case .Changed:
let newPoint = sender.locationInView(sender.view)
let deltaX = newPoint.x - startPoint.x
let deltaY = newPoint.y - startPoint.y
btn.center = CGPoint(x: btn.center.x + deltaX, y: btn.center.y + deltaY)
let index = indexOfPoint(btn.center, withButton: btn)
if index < 0 {
contain = false
} else {
UIView.animateWithDuration(0.2, animations: { () -> Void in
var temp = CGPointZero
let button = self.itemArray[index] as! UIButton
temp = button.center
button.center = self.originPoint
btn.center = temp
self.originPoint = btn.center
self.contain = true
})
}
case .Ended:
UIView.animateWithDuration(0.2, animations: { () -> Void in
btn.transform = CGAffineTransformIdentity
btn.alpha = 1.0
if self.contain != nil {
btn.center = self.originPoint
}
})
default:
break
}
}
func indexOfPoint(point: CGPoint, withButton btn: UIButton) -> Int {
for i in 0..<itemArray.count {
let button = itemArray[i] as! UIButton
if button != btn {
if CGRectContainsPoint(button.frame, point) {
return i
}
}
}
return -1
}
//隨機(jī)顏色
func randonColor() -> UIColor {
let randomR:CGFloat = CGFloat(drand48())
let randomG:CGFloat = CGFloat(drand48())
let randomB:CGFloat = CGFloat(drand48())
return UIColor(red: randomR, green: randomG, blue: randomB, alpha: 1.0)
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//使用
let dragView = DragSquaresView(frame: self.view.frame)
self.view.addSubview(dragView)
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者