How to display dummy data on swipe action on UIView

152 Views Asked by At

I have a containerView and inside that container view I have 2 UILabel(titleLabel and detailLabel) and 1 UIImageView(myImage)..I have added UI swipegesture to the containerView...Now I want to display 5 Dummy data (Text and Image) when I swipe on containerView..I have done this what should I do next ?

@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
    }
}

//to displayDummy data

class 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
}

}

1

There are 1 best solutions below

0
Adarsh K On

You can modify the last part of your swipe function the following way.

    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
    }

This is just for making it work. The code can be refactored and optimised.