gpt4 book ai didi

ios - 如何使用 "swipe"在 swift 中控制段?

转载 作者:行者123 更新时间:2023-11-28 16:07:51 25 4
gpt4 key购买 nike

我在检测滑动时遇到问题。

我试图做到这一点,如果用户向左滑动,那么我的 selectedSegmentIndex 会增加:

segment.selectedSegmentIndex+1

如果他们向右滑动,那么它会递减:

segment.selectedSegmentIndex-1

即用户不仅可以点击segment换页,还可以左右滑动改变“segment selectedSegmentIndex”

我怎样才能做到这一点?

下面是我为这个项目编写的代码:

class RecipesTableViewController: UITableViewController {
@IBOutlet var myTableView: UITableView!
@IBOutlet var menuButton:UIBarButtonItem!
@IBOutlet weak var segment: UISegmentedControl!


var recipes:[Recipe] = [
Recipe(name: "巧克力手工餅乾", type: "甜點", material: "1.無鹽奶油 150g\n2.低筋麵粉 275g\n3.可可粉 25g\n4.蛋 1顆\n5.細砂糖 100g\n6.泡打粉(可省) 1/2小匙", image: "ChocolateCookie.jpg", isFavor: false, amount:"", userPhoto:"小璧.jpg", userName:"小璧" ),
//Recipes(name: "巧克力手工餅乾", type: "餅乾", material: "1.無鹽奶油\n2.低筋麵粉\n3.可可粉\n4.蛋\n5.細砂糖\n6.泡打粉(可省)", image: "ChocolateCookie.jpg", isVisited: false, amount:"150g\n275g\n25g\n1顆\n100g\n1/2小匙"),
Recipe(name: "辣味雞肉義大利麵", type: "麵食", material: "1.義大利麵\n2.雞胸肉\n3.洋蔥\n4.白酒\n5.蘆筍\n6.鴻喜菇\n7.玉米筍\n8.甜椒\n9.鮮奶油\n10.奶油\n11.蒜碎\n12.花椒油\n13.義大利香料", image: "pasta.jpg", isFavor: false, amount:"180g\n50g\n15g\n50g\n15g\n10g\n10g\n5g\n150g\n10g\n5g\n20g\n2g", userPhoto:"Adrian Hsiao.jpg", userName:"Adrian Hsiao" ),
Recipe(name: "巧克力戚風蛋糕(6吋)", type: "甜點", material: "1.低筋麵粉\n2.無糖可可粉\n3.植物油\n4.牛奶\n5.蛋黃\n6.蛋白\n7.細砂糖\n8.蛋糕裝飾(奶油)", image: "chocolateCake.jpg", isFavor: false, amount:"45g\n15g\n30g\n60g\n3個\n3個\n45g\n適量", userPhoto:"維尼Winnie.jpg", userName:"維尼Winnie" ),
Recipe(name: "蜂蜜法式吐司磚", type: "早餐", material: "1.吐司\n2.蛋\n3.牛奶\n4.蜂蜜\n5.糖粉", image: "toaste.jpg", isFavor: false, amount:"2片\n1顆\n20CC\n依喜好\n依喜好", userPhoto:"Nico Sun 老孫妮可.jpg", userName:"Nico Sun 老孫妮可" ),
Recipe(name: "焗烤海鮮筆管麵", type: "麵食", material: "1.筆管麵\n 2.洋蔥\n3.蛤蜊\n4.透抽\n5.鮮蝦\n6.蒜頭\n7.水\n8.康寶濃湯粉\n9.鹽\n10.起司絲\n11.煮熟綠花椰菜", image: "pasta2.jpg", isFavor: false, amount:"1/3包\n1顆\n適量\n適量\n適量\n三小瓣\n1杯半\n半包\n少許\n適量\n適量", userPhoto:"阿渝師~美味廚房.jpg", userName:"阿渝師~美味廚房" ),
Recipe(name: "秀珍菇麻油雞", type: "冬令進補", material: "1.秀珍菇\n2.帶骨雞腿\n3.米酒\n4.麻油\n5.薑", image: "large_3649cc5a24aef9ec.jpg", isFavor: false, amount:"450g\n2隻\n600c.c\n6大匙\n1大塊", userPhoto:"小舖の廚房.jpg", userName:"小舖の廚房" ),
Recipe(name: "清熱除痘湯", type: "夏日清爽", material: "1.花旗參\n2.蒟蒻\n3.豬肉\n4.紅棗\n5.薑\n6.鹽\n7.水", image: "soap.jpg", isFavor: false, amount:"23克\n38克\n150克\n4粒\n1片\n1茶匙\n10杯", userPhoto:"唐安麒超猛瘦身食譜.jpg", userName:"唐安麒超猛瘦身食譜" ),
Recipe(name: "蜂蜜法式吐司磚", type: "早餐", material: "1.吐司\n2.蛋\n3.牛奶\n4.蜂蜜\n5.糖粉", image: "toaste.jpg", isFavor: false, amount:"2片\n1顆\n20CC\n依喜好\n依喜好", userPhoto:"Nico Sun 老孫妮可.jpg", userName:"Nico Sun 老孫妮可" ),
Recipe(name: "蜂蜜法式吐司磚", type: "早餐", material: "1.吐司\n2.蛋\n3.牛奶\n4.蜂蜜\n5.糖粉", image: "toaste.jpg", isFavor: false, amount:"2片\n1顆\n20CC\n依喜好\n依喜好", userPhoto:"Nico Sun 老孫妮可.jpg", userName:"Nico Sun 老孫妮可" ),
Recipe(name: "蜂蜜法式吐司磚", type: "早餐", material: "1.吐司\n2.蛋\n3.牛奶\n4.蜂蜜\n5.糖粉", image: "toaste.jpg", isFavor: false, amount:"2片\n1顆\n20CC\n依喜好\n依喜好", userPhoto:"Nico Sun 老孫妮可.jpg", userName:"Nico Sun 老孫妮可" )

]

var noodles:[Recipe] = [
Recipe(name: "辣味雞肉義大利麵", type: "麵食", material: "1.義大利麵\n2.雞胸肉\n3.洋蔥\n4.白酒\n5.蘆筍\n6.鴻喜菇\n7.玉米筍\n8.甜椒\n9.鮮奶油\n10.奶油\n11.蒜碎\n12.花椒油\n13.義大利香料", image: "pasta.jpg", isFavor: false, amount:"180g\n50g\n15g\n50g\n15g\n10g\n10g\n5g\n150g\n10g\n5g\n20g\n2g", userPhoto:"Adrian Hsiao.jpg", userName:"Adrian Hsiao" ),
Recipe(name: "焗烤海鮮筆管麵", type: "麵食", material: "1.筆管麵\n 2.洋蔥\n3.蛤蜊\n4.透抽\n5.鮮蝦\n6.蒜頭\n7.水\n8.康寶濃湯粉\n9.鹽\n10.起司絲\n11.煮熟綠花椰菜", image: "pasta2.jpg", isFavor: false, amount:"1/3包\n1顆\n適量\n適量\n適量\n三小瓣\n1杯半\n半包\n少許\n適量\n適量", userPhoto:"阿渝師~美味廚房.jpg", userName:"阿渝師~美味廚房" )
]

var snacks:[Recipe] = [
Recipe(name: "巧克力手工餅乾", type: "甜點", material: "1.無鹽奶油 150g\n2.低筋麵粉 275g\n3.可可粉 25g\n4.蛋 1顆\n5.細砂糖 100g\n6.泡打粉(可省) 1/2小匙", image: "ChocolateCookie.jpg", isFavor: false, amount:"", userPhoto:"小璧.jpg", userName:"小璧" ),
Recipe(name: "巧克力戚風蛋糕(6吋)", type: "甜點", material: "1.低筋麵粉\n2.無糖可可粉\n3.植物油\n4.牛奶\n5.蛋黃\n6.蛋白\n7.細砂糖\n8.蛋糕裝飾(奶油)", image: "chocolateCake.jpg", isFavor: false, amount:"45g\n15g\n30g\n60g\n3個\n3個\n45g\n適量", userPhoto:"維尼Winnie.jpg", userName:"維尼Winnie" )
]
//var recipesIsDone = [Bool](count: 21, repeatedValue: false)

@IBAction func changePage(sender: AnyObject) {
if segment.selectedSegmentIndex == 0 {
self.myTableView.reloadData()
self.category = 0
print(category)
//self.performSegueWithIdentifier("showRecipesDetail", sender: self)
}
if segment.selectedSegmentIndex == 1 {
self.myTableView.reloadData()
self.category = 1
print(category)
//self.performSegueWithIdentifier("showRecipesDetail", sender: self)
}
if segment.selectedSegmentIndex == 2 {
self.myTableView.reloadData()
self.category = 2
//self.performSegueWithIdentifier("showRecipesDetail", sender: self)
}
if segment.selectedSegmentIndex == 3 {
self.myTableView.reloadData()
self.category = 3
//self.performSegueWithIdentifier("showRecipesDetail", sender: self)
}
if segment.selectedSegmentIndex == 4 {
self.myTableView.reloadData()
self.category = 4
// self.performSegueWithIdentifier("showRecipesDetail", sender: self)
}
}

override func viewDidLoad() {
super.viewDidLoad()
//移除返回按鈕的標題
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回", style: .Plain, target: nil, action: nil)

self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

if revealViewController() != nil {
// revealViewController().rearViewRevealWidth = 62
menuButton.target = revealViewController()
menuButton.action = "revealToggle:"

revealViewController().rightViewRevealWidth = 150

view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}

//下兩個為建立tableView cell時必須建立的兩個function
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section
// if segment.selectedSegmentIndex == 1 {
//}
if segment.selectedSegmentIndex == 0 {
return self.recipes.count
}
if segment.selectedSegmentIndex == 1 {
return self.noodles.count
}
if segment.selectedSegmentIndex == 2 {
return self.snacks.count
}
if segment.selectedSegmentIndex == 3 {
return self.noodles.count
}
if segment.selectedSegmentIndex == 4 {
return self.noodles.count
}
return self.recipes.count

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell

if segment.selectedSegmentIndex == 0 {
let recipe = recipes[indexPath.row]
cell.favorIconImageView.hidden = !recipe.isFavor
cell.nameLabel.text = recipe.name
cell.thumbnailImageView.image = UIImage(named: recipe.image)
// cell.locationLabel.text = recipe.material
cell.typeLabel.text = recipe.type
cell.userPhoto.image = UIImage(named: recipe.userPhoto)
cell.userName.text = recipe.userName
}


if segment.selectedSegmentIndex == 1 {
let noodle = noodles[indexPath.row]
cell.favorIconImageView.hidden = !noodle.isFavor
cell.nameLabel.text = noodle.name
cell.thumbnailImageView.image = UIImage(named: noodle.image)
// cell.locationLabel.text = recipe.material
cell.typeLabel.text = noodle.type
cell.userPhoto.image = UIImage(named: noodle.userPhoto)
cell.userName.text = noodle.userName
}

if segment.selectedSegmentIndex == 2 {
let snack = snacks[indexPath.row]
cell.nameLabel.text = snack.name
cell.thumbnailImageView.image = UIImage(named: snack.image)
// cell.locationLabel.text = recipe.material
cell.typeLabel.text = snack.type
cell.userPhoto.image = UIImage(named: snack.userPhoto)
cell.userName.text = snack.userName
}

if segment.selectedSegmentIndex == 3 {
let noodle = noodles[indexPath.row]
cell.nameLabel.text = noodle.name
cell.thumbnailImageView.image = UIImage(named: noodle.image)
// cell.locationLabel.text = recipe.material
cell.typeLabel.text = noodle.type
cell.userPhoto.image = UIImage(named: noodle.userPhoto)
cell.userName.text = noodle.userName
}

if segment.selectedSegmentIndex == 4 {
let noodle = noodles[indexPath.row]
cell.nameLabel.text = noodle.name
cell.thumbnailImageView.image = UIImage(named: noodle.image)
// cell.locationLabel.text = recipe.material
cell.typeLabel.text = noodle.type
cell.userPhoto.image = UIImage(named: noodle.userPhoto)
cell.userName.text = noodle.userName
}
//I've been here
//cell.accessoryType = recipesIsDone[indexPath.row] ? .Checkmark : .None
//也可寫成
/*
if restaurantIsVisited[indexPath.rwo] {
cell?.accessoryType = .Checkmark
} else {
cell?.accessoryType = .None
}
*/


//隱藏愛心圖片
//cell.favorIconImageView.hidden = !recipe.isFavor


/*
//Circular image
//cell.thumbnailImageView.layer.cornerRadius = 10.0
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width / 2
cell.thumbnailImageView.clipsToBounds = true
*/


return cell
}


//滑動刪除
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

if editingStyle == .Delete {

if segment.selectedSegmentIndex == 0{
self.recipes.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if segment.selectedSegmentIndex == 1{
self.noodles.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if segment.selectedSegmentIndex == 2{
self.snacks.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if segment.selectedSegmentIndex == 3{
self.noodles.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if segment.selectedSegmentIndex == 4{
self.noodles.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}



}
}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let doneAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "收藏", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell

switch self.segment.selectedSegmentIndex {
case 0:
let recipe = self.recipes[indexPath.row]
recipe.isFavor = recipe.isFavor ? false : true
cell.favorIconImageView.hidden = !recipe.isFavor
print(recipe.isFavor)
case 1:
let noodle = self.noodles[indexPath.row]
noodle.isFavor = noodle.isFavor ? false : true
cell.favorIconImageView.hidden = !noodle.isFavor
print(noodle.isFavor)
case 2:
let snack = self.snacks[indexPath.row]
snack.isFavor = snack.isFavor ? false : true
cell.favorIconImageView.hidden = !snack.isFavor
print(snack.isFavor)

default:
/*let recipe = self.recipes[indexPath.row]
recipe.isFavor = recipe.isFavor ? false : true
cell.favorIconImageView.hidden = !recipe.isFavor
print(recipe.isFavor)*/
let noodle = self.noodles[indexPath.row]
noodle.isFavor = noodle.isFavor ? false : true
cell.favorIconImageView.hidden = !noodle.isFavor
print(noodle.isFavor)
}
/*let recipe = self.recipes[indexPath.row]
recipe.isFavor = recipe.isFavor ? false : true

print(recipe.isFavor)*/

//cell.accessoryType = self.recipesIsDone[indexPath.row] ? .Checkmark : .None


}
)

var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete",handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

//刪除列的資料
switch self.segment.selectedSegmentIndex {
case 0:
self.recipes.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

case 1:
self.noodles.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
case 2:
self.snacks.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

default:
self.noodles.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}



})

//改變滑動按鈕的背景顏色
doneAction.backgroundColor = UIColor(red: 255.0/255.0, green: 166.0/255.0, blue: 51.0/255.0, alpha: 1.0)
//deleteAction.backgroundColor = UIColor(red: 51.0/255.0, green: 51.0/255.0, blue: 51.0/255.0, alpha: 1.0)

//告訴表格滑動時要產生share跟delete的按鈕
return [deleteAction, doneAction]
}

var category: Int = Int()
//var : Int = Int()
//segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "showRecipesDetail" {
// var obj = segue.destinationViewController as! DetailTableViewCell
if let indexPath = self.tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as! DetailViewController

if segment.selectedSegmentIndex == 0 {
destinationController.recipes = recipes[indexPath.row]
destinationController.category = self.category
print(category)
}
if segment.selectedSegmentIndex == 1 {
destinationController.noodles = noodles[indexPath.row]
destinationController.category = self.category
print(category)
}
if segment.selectedSegmentIndex == 2 {
destinationController.snacks = snacks[indexPath.row]
destinationController.category = self.category
print(category)
}
if segment.selectedSegmentIndex == 3 {
destinationController.noodles = noodles[indexPath.row]
destinationController.category = self.category
}
if segment.selectedSegmentIndex == 4 {
destinationController.noodles = noodles[indexPath.row]
destinationController.category = self.category
}
}
}

}




/*
//滑動隱藏導覽列
override func viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)
self.navigationController?.hidesBarsOnSwipe = true

}
*/



}

最佳答案

您可能需要根据您的项目稍微调整以下答案,因为我仍然不完全确定您要做什么,但这应该是您正在寻找的一般想法:

在viewDidLoad中:

 var swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedRight))
swipeRight.direction = UISwipeGestureRecognizerDirection.Right

var swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedLeft))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left

在你的 View Controller 中:

func swipedRight(){
segment.selectedSegmentIndex -= 1
}

func swipedLeft(){
segment.selectedSegmentIndex += 1
}

添加滑动手势识别器后,它们将在用户滑动时触发操作,调用上述函数,这将增加或减少您的变量。

注意:

此外,这与您的问题无关,但我注意到您的代码中有很多冗余。你真的应该考虑尝试更有效地执行你的一些任务。有时在您的代码中有大量的 if 语句,每个括号内都有相同的代码,这是没有意义的。

例如这段代码:

if segment.selectedSegmentIndex == 0{
self.recipes.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if segment.selectedSegmentIndex == 1{
self.noodles.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if segment.selectedSegmentIndex == 2{
self.snacks.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if segment.selectedSegmentIndex == 3{
self.noodles.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
if segment.selectedSegmentIndex == 4{
self.noodles.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}

应该能够在该函数中像这样表达并得到相同的结果:

self.noodles.removeAtIndex(indexPath.row)
//self.tableView.reloadData()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

您也可以尝试为变量设置值,例如

self.category = segment.selectedSegmentIndex 

这样您就不必评估每个值。只是一个想法,但我认为它确实有助于提高代码的可读性。

关于ios - 如何使用 "swipe"在 swift 中控制段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39979645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com