我有一个containerView,在这个容器视图中,我有2个UILabel(titleLabel和detailLabel(和1个UIImageView(myImage(。。我已将UI滑动菜单添加到容器视图中。。。现在,当我在containerView上滑动时,我想显示5个虚拟数据(文本和图像(。。我已经做完了,下一步该怎么办?
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var containerVIew: UIView!
var dataArray :[DummyClass] = []
var swipeAction:Int = 0
override func viewDidAppear(_ animated: Bool)
{
leftSwipe()
rightSwipe()
let DD1 = DummyClass(title: "Tobey", detail: "Lives at russia ", image: "Image1")
let DD2 = DummyClass(title: "Jessy", detail: "Lives at India", image: "Image2")
let DD3 = DummyClass(title: "Smith", detail: "Lives at Dubai", image: "Image3")
let DD4 = DummyClass(title: "Mark", detail: "Lives at Australia", image: "Image4")
let DD5 = DummyClass(title: "Henry", detail: "Lives at America", image: "Image5")
userArray.append(DD1)
userArray.append(DD2)
userArray.append(DD3)
userArray.append(DD4)
userArray.append(DD5)
}
//MARK:Left Swipe Function
func leftSwipe()
{
let swipeLeft = UISwipeGestureRecognizer()
swipeLeft.direction = .left //up
self.insideContainerViewSecond.addGestureRecognizer(swipeLeft)
swipeLeft.addTarget(self, action: #selector(swipe(sender:)))
}
//MARK:Right Swipe Function
func rightSwipe()
{
let swipeRight = UISwipeGestureRecognizer()
swipeRight.direction = .right //down
self.insideContainerViewSecond.addGestureRecognizer(swipeRight)
swipeRight.addTarget(self, action: #selector(swipe(sender:)))
}
//MARK: Swipe Function
@objc func swipe(sender: UISwipeGestureRecognizer)
{
switch sender.direction
{
case .left:
print("swiped left")
swipeAction = swipeAction+1
case .right:
print("swiped right")
swipeAction = swipeAction-1
default:
print("no action")
}
if (swipeAction < 0)
{
swipeAction = 0
}
if (swipeAction>userArray.count - 1)
{
swipeAction = userArray.count-1
}
}
//显示伪数据
类DummyClass{
var title:String=""
var detail:String=""
var image:String=""
init()
{
}
init(title:String,detail:String,image:String)
{
self.title=title
self.detail=detail
self.image=image
}
}
您可以通过以下方式修改滑动功能的最后部分。
if (swipeAction < 0) {
swipeAction = 0
} else if (swipeAction>userArray.count - 1) {
swipeAction = userArray.count-1
} else {
let user = userArray[swipeAction]
titleLabel.text = user.title
detailLabel.text = user.detail
myImage.image = user.image // This should be an image though. In your Dummy class its a string
}
这只是为了让它发挥作用。代码可以进行重构和优化。