I am building a quote app using TGLParallaxCarousel library in my project. I try to custom the CustomView
of TGLParallaxCarouselItem by adding two UIButtons
(favButton and shareButton) on it.
I am able to change the UIButton looks based on its state--whether the current quote is faved or not, by doing this (in CustomView.swift):
convenience init(frame: CGRect, number: Int) {
self.init(frame: frame)
currentQuote = quoteData[number]
favButton.tag = number
currentQuote.faved == true ? favButton.setImage(#imageLiteral(resourceName: "fav-on"), for: .normal) : favButton.setImage(#imageLiteral(resourceName: "fav-off"), for: .normal)
}
However I need to be able to turn the fav on and off by clicking the favButton
. I tried to connect the favButton
directly as an IBAction
to the XIB file, but it's not working. I still can't access the favButton touchUpInside
state.
I've tried addTarget
on favButton. It's not working. My tap is detected as tap on CustomView
rather than specifically on favButton.
Here's the detectTap
function that fired when I tap anywhere on the CustomView (including on the favButton). This function is within the TGLParallaxCarousel.swift:
func detectTap(_ recognizer:UITapGestureRecognizer) {
let targetPoint: CGPoint = recognizer.location(in: recognizer.view)
currentTargetLayer = mainView.layer.hitTest(targetPoint)!
guard let targetItem = findItemOnScreen() else { return }
let firstItemOffset = (items.first?.xDisp ?? 0) - targetItem.xDisp
let tappedIndex = -Int(round(firstItemOffset / xDisplacement))
self.delegate?.carouselView(self, didSelectItemAtIndex: tappedIndex)
if targetItem.xDisp == 0 {
self.delegate?.carouselView(self, didSelectItemAtIndex: tappedIndex)
}
else {
selectedIndex = tappedIndex
}
}
Please help, what should I do?