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
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來譬挚,“玉大人锅铅,你說我怎么就攤上這事〖跣” “怎么了盐须?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長漆腌。 經(jīng)常有香客問我贼邓,道長,這世上最難降的妖魔是什么闷尿? 我笑而不...
- 正文 為了忘掉前任塑径,我火速辦了婚禮,結(jié)果婚禮上填具,老公的妹妹穿的比我還像新娘统舀。我一直安慰自己,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布誉简。 她就那樣靜靜地躺著碉就,像睡著了一般。 火紅的嫁衣襯著肌膚如雪闷串。 梳的紋絲不亂的頭發(fā)上瓮钥,一...
- 文/蒼蘭香墨 我猛地睜開眼只损,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼一姿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起跃惫,我...
- 序言:老撾萬榮一對(duì)情侶失蹤叮叹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后爆存,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛉顽,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年先较,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了携冤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站癌幕,受9級(jí)特大地震影響衙耕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜勺远,卻給世界環(huán)境...
- 文/蒙蒙 一臭杰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧谚中,春花似錦、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至某筐,卻和暖如春比搭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像装处,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- Now 項(xiàng)目完成上線后穴亏,需要給所有的TableView列表頁沒有數(shù)據(jù)的時(shí)候添加一個(gè)友好的提示圖,如果一個(gè)個(gè)的去改的...
- UITableViewController *tvc =[[UITableViewController alloc...
- iOS 7及之前: 先依次調(diào)一遍heightForRow方法再依次調(diào)一遍cellForRow方法刺覆,在調(diào)cellFo...
- 看到標(biāo)題也許你會(huì)疑問谦屑,自己理想中的樣子到底是什么樣的,其實(shí)這個(gè)問題特別廣泛劲室,答案也天馬行空伦仍,糾結(jié)其原因,只是因?yàn)檫@...