import UIKit
class ViewController: UIViewController {
private var sourceIndexPath: NSIndexPath!
private var fromIndexPath: NSIndexPath!
private var snapView : UIView!
private var indexPath: NSIndexPath!
private var heros : [Heros]?{
didSet{
}
}
@IBOutlet weak var baseTableView: BaseTableView!
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("heros", ofType: "plist")
let array = NSArray(contentsOfFile: path!) as! [[String: AnyObject]]
heros = array.map{Heros(dict:$0)}
baseTableView.estimatedRowHeight = 44
baseTableView.rowHeight = UITableViewAutomaticDimension
GestrureAdd()
}
func GestrureAdd() {
let LoogPress = UILongPressGestureRecognizer(target: self, action:"addGestureStats:")
LoogPress.minimumPressDuration = 1
baseTableView.addGestureRecognizer(LoogPress)
}
@objc func addGestureStats(LoogPress: UILongPressGestureRecognizer) {
if LoogPress.state == UIGestureRecognizerState.Began {
addGesturStatsBegan(LoogPress)
}
if LoogPress.state == UIGestureRecognizerState.Changed {
addGesturStatsChanged(LoogPress)
}
if LoogPress.state == UIGestureRecognizerState.Ended || LoogPress.state == UIGestureRecognizerState.Cancelled {
addGesturStatsEndedAndCancelled(LoogPress)
}
}
func addGesturStatsBegan(LoogPerss: UILongPressGestureRecognizer) {
let point = LoogPerss.locationInView(baseTableView)
indexPath = baseTableView.indexPathForRowAtPoint(point)
if let cell = baseTableView.cellForRowAtIndexPath(indexPath) {
sourceIndexPath = indexPath
let snapView = self.snapView(cell)
self.snapView = snapView
baseTableView.addSubview(snapView)
UIView.animateWithDuration(0.25, animations: {
// 选中Cell跳出放大效果
snapView.alpha = 0.95
snapView.center = CGPointMake(cell.center.x, point.y)
snapView.transform = CGAffineTransformMakeScale(1.05, 1.05)
cell.alpha = 0
}, completion: { (completed) -> Void in
cell.hidden = true
cell.alpha = 1
})
}else{
sourceIndexPath = nil
snapView = nil
}
fromIndexPath = NSIndexPath(index: 0)
fromIndexPath = sourceIndexPath
}
func addGesturStatsChanged(LoogPerss: UILongPressGestureRecognizer) {
let point = LoogPerss.locationInView(baseTableView)
indexPath = baseTableView.indexPathForRowAtPoint(point)
snapView.center = CGPointMake(snapView.center.x, point.y)
fromIndexPath = NSIndexPath(index: 0)
fromIndexPath = sourceIndexPath
if fromIndexPath != indexPath {
baseTableView.beginUpdates()
let temp = heros![indexPath.row]
heros![indexPath.row] = heros![fromIndexPath.row]
heros![fromIndexPath.row] = temp
baseTableView.moveRowAtIndexPath(fromIndexPath, toIndexPath: indexPath)
baseTableView.endUpdates()
sourceIndexPath = indexPath
}
let stop: CGFloat = 64
if let parenView = baseTableView.superview{
let parenPos = baseTableView.convertPoint(point, toView: parenView)
if parenPos.y > parenView.bounds.height - stop {
var offset = baseTableView.contentOffset
offset.y += (parenPos.y - baseTableView.bounds.height + stop)
print(parenPos.y - baseTableView.bounds.height + stop)
if offset.y > baseTableView.contentSize.height - baseTableView.bounds.height {
offset.y = baseTableView.contentSize.height - baseTableView.bounds.height
}
baseTableView.setContentOffset(offset, animated: false)
} else if parenPos.y <= stop {
var offset = baseTableView.contentOffset
offset.y -= (stop - parenPos.y)
if offset.y < 0 {
offset.y = 0
}
baseTableView.setContentOffset(offset, animated: false)
}
}
}
func addGesturStatsEndedAndCancelled(LoogPerss: UILongPressGestureRecognizer) {
let cell = baseTableView.cellForRowAtIndexPath(fromIndexPath)!
cell.alpha = 0
cell.hidden = false
UIView.animateWithDuration(0.25, animations: {
self.snapView.center = cell.center
self.snapView.alpha = 0
cell.alpha = 1
}) { (_) in
self.snapView = nil
self.sourceIndexPath = nil
self.baseTableView.performSelector("reloadData", withObject: nil, afterDelay: 0.5)
}
}
func snapView(view: UIView) -> UIImageView {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let snapShot = UIImageView(image: image)
snapShot.layer.masksToBounds = false;
snapShot.layer.cornerRadius = 0;
snapShot.layer.shadowOffset = CGSizeMake(-10.0, 0.0);
snapShot.layer.shadowOpacity = 0.4;
snapShot.layer.shadowRadius = 5;
snapShot.frame = view.frame
return snapShot
}
}
extension ViewController : UITableViewDataSource{
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return heros!.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! HeroTableViewCell
let models = heros![indexPath.row]
cell.heros = models
return cell
}
}
交换的tableView
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Now 项目完成上线后,需要给所有的TableView列表页没有数据的时候添加一个友好的提示图,如果一个个的去改的...
- UITableViewController *tvc =[[UITableViewController alloc...
- iOS 7及之前: 先依次调一遍heightForRow方法再依次调一遍cellForRow方法,在调cellFo...