CollectionViewCell will not display dynamic images - ios

I am creating a flip animation on the screen. On view load the page shows the back of the card fine. all 16 images are showing the back of the card. After clicking the button to flip, I need the CollectionViewCell to show different images (card_1.jpg, card_2.jpg, etc..) but only the first cell displays the image.
class CardsCollectionViewCell: UICollectionViewCell {
var deck: [Int] = []
#IBOutlet weak var numberLabel: UILabel!
let cardBackTag: Int = 0
let cardFrontTag: Int = 1
var cardViews: (frontView: UIImageView, backView: UIImageView)?
var imgViewFront: UIImageView!
var imgViewBack: UIImageView!
override func awakeFromNib() {
self.imgViewFront = self.createCardViewWithImage(imageName: "card_1", tag: self.cardFrontTag)
self.imgViewBack = self.createCardViewWithImage(imageName: "back-card", tag: self.cardBackTag)
self.cardViews = (frontView: self.imgViewFront, backView: self.imgViewBack)
self.contentView.addSubview(self.imgViewFront)
}
private func createCardViewWithImage(imageName: String, tag: Int) -> UIImageView {
let newCardImageView = UIImageView(frame: self.frame)
newCardImageView.image = UIImage(named: imageName)
newCardImageView.tag = tag
newCardImageView.clipsToBounds = true
newCardImageView.contentMode = .scaleAspectFill
return newCardImageView
}
func flipCardAnimation(indexPath: Int) {
if (self.imgViewBack.superview != nil) {
// front card
self.numberLabel.textColor = UIColor.white
self.numberLabel.backgroundColor = UIColor.orange
self.numberLabel.clipsToBounds = true
self.numberLabel.text = "\(self.deck[indexPath])"
self.numberLabel.isHidden = false
self.cardViews!.frontView.removeFromSuperview()
self.imgViewFront = self.createCardViewWithImage(imageName: "card_\(self.deck[indexPath])", tag: self.cardFrontTag)
self.cardViews = (frontView: self.imgViewFront, backView: self.imgViewBack)
} else {
// back card
self.deck = [Int](repeating: 0, count: 16)
self.numberLabel.isHidden = true
self.cardViews = (frontView: self.imgViewBack, backView: self.imgViewFront)
}
let transitionOptions = UIView.AnimationOptions.transitionFlipFromLeft
UIView.transition(with: self.contentView, duration: 0.5, options: transitionOptions, animations: {
self.cardViews!.backView.removeFromSuperview()
self.contentView.addSubview(self.cardViews!.frontView)
}, completion: {finished in
})
}
}
I followed this articles example to create the animation. https://medium.com/#lawrey/creating-a-flip-card-animation-with-uicollectionviewcell-swift-3-0-98bc96317fee
In my code above I have a label which displays the number at the indexPath inside the deck array. but not the image can someone tell me how to display all image dynamically? or what am I doing wrong?

So after spending a whole day yesterday I figured it's time to change my approach. I decided to create two images in the storyboard instead. Here is my final working code. This load all 16 cards with the back of the card photo and when a button is clicked the flip animation flips to random 16 card.
import UIKit
import Foundation
import ProgressHUD
class CardsCollectionViewCell: UICollectionViewCell {
var deck: [Int] = []
#IBOutlet weak var numberLabel: UILabel!
#IBOutlet weak var imgViewBack: UIImageView!
#IBOutlet weak var imgViewFront: UIImageView!
var cardBack: Bool = true
func flipCardAnimation(indexPath: Int) {
numberLabel.isHidden = true
let transitionOptions = UIView.AnimationOptions.transitionFlipFromLeft
UIView.transition(with: self.contentView, duration: 0.5, options: transitionOptions, animations: {
if self.cardBack == true {
self.deck = [Int](repeating: 0, count: NUMBER_OF_CARDS_IN_DECK)
self.imgViewBack.isHidden = false
self.imgViewFront.isHidden = true
} else {
self.numberLabel.isHidden = false
self.imgViewBack.isHidden = true
self.imgViewFront.isHidden = false
}
if (self.cardBack != true) {
// front card
self.numberLabel.text = "\(self.deck[indexPath])"
self.numberLabel.textColor = UIColor.white
self.numberLabel.backgroundColor = .red
self.numberLabel.layer.cornerRadius =
self.numberLabel.layer.frame.width/2
self.numberLabel.layer.masksToBounds = true
self.imgViewFront.image = UIImage(named: "card_\(self.deck[indexPath])")
self.imgViewFront.tag = self.deck[indexPath]
self.imgViewFront.contentMode = .scaleAspectFill
self.imgViewFront.clipsToBounds = true
self.imgViewFront.layer.cornerRadius = 12
} else {
// back card
print(self.deck)
self.numberLabel.text?.removeAll()
self.imgViewBack.image = UIImage(named: "back-card-1")
self.imgViewBack.tag = 1
self.imgViewBack.contentMode = .scaleAspectFill
self.imgViewBack.clipsToBounds = true
}
}, completion: {finished in
if self.cardBack == true {
self.cardBack = false
} else {
self.cardBack = true
}
})
}
}

Related

My UITapGestureRecognizer affects all elements in TableViewCell

I am trying to do my first ever App, and i'm need to add UITapGestureRecognizer that will scale photo in TableViewCell.
And it is works, but not correctly — my Gesture scale not just photo in ViewCell but all elements in ViewCell. How could I fix this?
All my code for TableViewCell:
import UIKit
class PhotosCollectionViewCell: UICollectionViewCell {
static let identifier = "PhotosCollectionViewCell"
#IBOutlet weak var mainPhoto: UIImageView!
#IBOutlet weak var mainLabel: UILabel!
var isInScale: Bool = false
func configure(photo: PhotosInProfile) {
mainPhoto.image = UIImage(named: photo.mainPhoto)
mainLabel.text = "Date: \(photo.date)"
}
override func awakeFromNib() {
super.awakeFromNib()
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(imageDoubleTapped))
doubleTap.numberOfTapsRequired = 2
mainPhoto.isUserInteractionEnabled = true
mainPhoto.addGestureRecognizer(doubleTap)
}
#objc func imageDoubleTapped() {
if isInScale {
self.transform = .identity
} else {
self.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}
isInScale.toggle()
}
}
import UIKit
class PhotosCollectionViewCell: UICollectionViewCell {
static let identifier = "PhotosCollectionViewCell"
#IBOutlet weak var mainPhoto: UIImageView!
#IBOutlet weak var mainLabel: UILabel!
var isInScale: Bool = false
func configure(photo: PhotosInProfile) {
mainPhoto.image = UIImage(named: photo.mainPhoto)
mainLabel.text = "Date: \(photo.date)"
}
override func awakeFromNib() {
super.awakeFromNib()
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(imageDoubleTapped))
doubleTap.numberOfTapsRequired = 2
mainPhoto.isUserInteractionEnabled = true
mainPhoto.addGestureRecognizer(doubleTap)
}
#objc func imageDoubleTapped() {
if isInScale {
mainPhoto.transform = .identity
} else {
mainPhoto.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}
isInScale.toggle()
}
}

UITableViewCell constraints not updated till I scroll tableview

Following is my code to update view height inside tableview cell
override func layoutSubviews() {
super.layoutSubviews()
layout()
}
fileprivate func layout() {
rootFlexContainer.frame.size.width = frame.width - 20
rootFlexContainer.flex.layout(mode: .adjustHeight)
if(rootFlexContainer.frame.height != 0) {
tagsHeight.constant = rootFlexContainer.frame.height
}
}
although this is not reflected till I scroll i.e is cell is recreated. How to update constraint inside TableViewCell of a view?
here is entire tableviewcell code
import UIKit
import FlexLayout
import SDWebImage
class StoreListTableViewCell: UITableViewCell {
#IBOutlet weak var menuLbl: UILabel!
#IBOutlet weak var menuView: UIView!
#IBOutlet weak var cellBgview: UIView!
#IBOutlet weak var storeImg: UIImageView!
#IBOutlet weak var storeLocation: UILabel!
#IBOutlet weak var storeTitle: UILabel!
#IBOutlet weak var cellContainer: UIView!
#IBOutlet weak var stackView: UIStackView!
#IBOutlet weak var tagsHeight: NSLayoutConstraint!
var storeImgStr = ""
var storeTitleStr = ""
var storeLocationStr = ""
var score : Double = 0.0
var tagsStr = ""
var isMenuAvailble = 0
var retailerTags: [String]?
var cashbackString = ""
fileprivate let rootFlexContainer = UIView()
#IBOutlet weak var tagsView: UIView!
#IBOutlet weak var ratingBtn: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
self.layoutIfNeeded()
cellBgview.clipsToBounds = true
cellBgview.layer.cornerRadius = 5
cellContainer.layer.shadowColor = UIColor.lightGray.cgColor
cellContainer.layer.shadowOpacity = 0.5
cellContainer.layer.shadowRadius = 5.0
cellContainer.layer.shadowOffset = CGSize(width: 0, height: 2)
cellContainer.backgroundColor = UIColor.clear
cellContainer.layer.cornerRadius = 5.0
cellContainer.layer.borderColor = UIColor.white.cgColor
cellContainer.layer.borderWidth = 0.5
ratingBtn.layer.borderWidth = 1
ratingBtn.layer.cornerRadius = 5
tagsView.addSubview(rootFlexContainer)
//cellContainer.layer.shadowPath = UIBezierPath(rect: cellBgview.bounds).cgPath
}
func setSetupStoreUI() {
if isMenuAvailble == 1 {
menuView.isHidden = false
menuView.layer.cornerRadius = 10
} else {
menuView.isHidden = true
}
storeTitle.text = storeTitleStr
// storeTitle.sizeToFit()
storeLocation.text = storeLocationStr
//storeLocation.sizeToFit()
//.filter{ $0.name != " " }
//storeImg.sd_imageIndicator = SDWebImageActivityIndicator.gray
storeImg.backgroundColor = UIColor.hexStringToUIColor(hex: AppStrings.placeHolderColor)
if let url = URL(string: storeImgStr.encoded), !(storeImgStr.isEmpty) {
self.storeImg.sd_setImage(with: url)
}
menuLbl.text = AppLocalString.PREORDER_TEXT.localized()
menuLbl.font = FontStyle.ProximaNovaBold(size: 12)
storeTitle.font = FontStyle.ProximaNovaSemibold(size: 14)
storeLocation.font = FontStyle.ProximaNovaRegular(size: 12)
storeLocation.textColor = .gray
// ratingBtn.isHidden = (score == 0)
ratingBtn.setTitle(score == 0 ? "-" : String(format: "%.1f", score), for: .normal)
ratingBtn.backgroundColor = UIColor.hexStringToUIColor(hex: Utility.getRatingColor(rating: score))
ratingBtn.layer.borderColor = UIColor.hexStringToUIColor(hex: Utility.getRatingColor(rating: score)).cgColor
rootFlexContainer.subviews.forEach({ $0.removeFromSuperview() })
//tagsView.willRemoveSubview(rootFlexContainer)
//rootFlexContainer.frame = tagsView.frame
//tagsView.addSubview(rootFlexContainer)
rootFlexContainer.flex.direction(.row).wrap(.wrap).alignSelf(.auto).justifyContent(.start).paddingRight(2).define { (flex) in
for i in 0..<((retailerTags?.count ?? 0) > 3 ? 3 : (retailerTags?.count ?? 0)) {
let nameLabel = UIButton()
nameLabel.isUserInteractionEnabled = false
nameLabel.setTitle((retailerTags?[i] ?? "").trim(), for: .normal)
nameLabel.setTitleColor(.black, for: .normal)
nameLabel.titleLabel?.font = FontStyle.ProximaNovaRegular(size: 11)
nameLabel.contentEdgeInsets = UIEdgeInsets(top: 1.5, left: 4, bottom: 1.5, right:4)
nameLabel.layer.borderColor = UIColor.hexStringToUIColor(hex: AppStrings.grayBorderColor).cgColor
nameLabel.layer.cornerRadius = 8
nameLabel.layer.borderWidth = 1.0
nameLabel.sizeToFit()
flex.addItem(nameLabel).margin(2)
}
if cashbackString != "" {
let cashbackLabel = UIButton()
cashbackLabel.backgroundColor = UIColor.hexStringToUIColor(hex: AppStrings.orangeCashbackColor)
cashbackLabel.isUserInteractionEnabled = false
cashbackLabel.setTitle(cashbackString, for: .normal)
cashbackLabel.setTitleColor(.black, for: .normal)
cashbackLabel.titleLabel?.font = FontStyle.ProximaNovaRegular(size: 10)
cashbackLabel.contentEdgeInsets = UIEdgeInsets(top: 1.5, left: 5, bottom: 1.5, right: 5)
cashbackLabel.layer.cornerRadius = 5
cashbackLabel.layer.borderWidth = 0
cashbackLabel.sizeToFit()
flex.addItem(cashbackLabel).margin(2)
}
}
rootFlexContainer.flex.layout()
if retailerTags?.count ?? 0 == 0 {
tagsView.isHidden = true
} else {
tagsView.isHidden = false
}
}
override func layoutSubviews() {
super.layoutSubviews()
layout()
}
fileprivate func layout() {
rootFlexContainer.frame.size.width = frame.width - 20
rootFlexContainer.flex.layout(mode: .adjustHeight)
if(rootFlexContainer.frame.height != 0) {
tagsHeight.constant = rootFlexContainer.frame.height
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Following is view hierarchy from stroryboard
A few observations -
1. Unnecessary layoutIfNeeded() call inside awakeFromNib()
override func awakeFromNib() {
super.awakeFromNib()
/*
self.layoutIfNeeded() // Not Necessary, Remove it.
*/
// No other changes to current implementation
}
2. Following should not be necessary, remove it.
/* Not necessary, Remove it.
override func layoutSubviews() {
super.layoutSubviews()
layout()
}
fileprivate func layout() {
rootFlexContainer.frame.size.width = frame.width - 20
rootFlexContainer.flex.layout(mode: .adjustHeight)
if(rootFlexContainer.frame.height != 0) {
tagsHeight.constant = rootFlexContainer.frame.height
}
}
*/
3. In the method, where you populate all the details, make following changes
func setSetupStoreUI() {
// No changes to current implementation
/* Remove this, we will update later
rootFlexContainer.flex.layout()
*/
if retailerTags?.count ?? 0 == 0 {
tagsView.isHidden = true
} else {
tagsView.isHidden = false
}
// Copied as is from `layout()`
rootFlexContainer.frame.size.width = frame.width - 20
rootFlexContainer.flex.layout(mode: .adjustHeight)
if (rootFlexContainer.frame.height != 0) {
tagsHeight.constant = rootFlexContainer.frame.height
}
// do a manual layout (on contentView, NOT self)
self.contentView.layoutIfNeeded()
}
You have to tell the table view controller that your cell height has changed.
Most common methods are protocol/delegate pattern or (swift preferred) closures.
For example...
In your cell class:
class MyTableViewCell: UITableViewCell {
// "callback" closure
var cellHeightChanged: (()->())?
// whatever happens that you need to change the cell height
func heightChanged() -> Void {
tagsHeight.constant = rootFlexContainer.frame.height
cellHeightChanged?()
}
}
Then, in your controller, in cellForRowAt:
cell.cellHeightChanged = {
tableView.performBatchUpdates(nil, completion: nil)
}

One button with different actions each touch in iOS

I want to change the text (or the background or the animation or what ever) in each click, what is the best way to do it?
#IBOutlet weak var text: UILabel!
#IBOutlet var background: UIView!
#IBOutlet weak var button: UIButton!
#IBAction func play(_ sender: UIButton) {
UIView.animate(withDuration: 1.5) {
self.button.frame.origin.y = 30
self.button.frame.size.height = 200
self.button.frame.size.width = 130
self.button.backgroundColor = .yellow
self.text.textColor = .white
self.text.frame.origin.y += 110
self.background.backgroundColor = .black
self.text.text = "Thanks for grabiN."
// ..text = "Now click it until it's gone!"
// ..text = "ComeOn you can do iT"
// ..text = "Lest time"
}
}
create massive and one count param
var count = 0
var texts = ["Thanks for grabiN.", "ComeOn you can do iT"]
UIView.animate(withDuration: 1.5) {
self.button.frame.origin.y = 30
self.button.frame.size.height = 200
self.button.frame.size.width = 130
self.button.backgroundColor = .yellow
self.text.textColor = .white
self.text.frame.origin.y += 110
self.background.backgroundColor = .black
let index = self.count % self.texts.count
self.text.text = self.texts[index]
self.count+=1
}

How to apply an animation effect to UIImageView slideshow

On Swift 3.0 how can I apply some animation effect to this slideshow? Cannot find animation effects related to animationWithDuration method.
let image1 = UIImage(named: "image1")!
let image2 = UIImage(named: "image2")!
let image3 = UIImage(named: "image3")!
var imagesArray : [UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
imagesArray = [image1, image2, image3]
myView.clipsToBounds = true
myView.animationImages = imagesArray
myView.animationDuration = 10.0
myView.animationRepeatCount = 0
myView.startAnimating()
}
It looks like you are using the build in frame animation for UIImageView. This is designed to just cycle through the images, like an animated gif. It doesn't really have more sophisticated animation than that. What you can do if you want transition effects is alternate between two image views and use the UIView Animation methods to switch between them. This just does a crossfade by manipulating alpha:
var images = [UIImage]()
var currentImageindex = 0
func animateImageViews() {
swap(&firstImageView, &secondImageView)
secondImageView.image = images[currentImageindex]
currentImageindex = (currentImageindex + 1) % images.count
UIView.animate(withDuration: 1, animations: {
self.firstImageView.alpha = 0
self.secondImageView.alpha = 1
}, completion: { _ in
self.animateImageViews()
})
}
Here is playground that shows how it works. You need to drop 2 images into the resources folder named 1.png and 2.png for it to work. Note that setting the view frames like this is horrible programming practice i just did it here for brevity. use interface builder and autolayout in your actual code.
import PlaygroundSupport
import UIKit
class Demo: UIViewController {
let button = UIButton()
var firstImageView = UIImageView()
var secondImageView = UIImageView()
var images = [UIImage]()
var currentImageindex = 0
override func viewDidLoad() {
view.addSubview(firstImageView)
view.addSubview(secondImageView)
images.append(UIImage(named: "1.png")!)
images.append(UIImage(named: "2.png")!)
firstImageView.image = images[0]
secondImageView.image = images[1]
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
firstImageView.frame = view.frame
secondImageView.frame = view.frame
}
override func viewDidAppear(_ animated: Bool) {
animateImageViews()
}
func animateImageViews() {
swap(&firstImageView, &secondImageView)
secondImageView.image = images[currentImageindex]
currentImageindex = (currentImageindex + 1) % images.count
UIView.animate(withDuration: 1, animations: {
self.firstImageView.alpha = 0
self.secondImageView.alpha = 1
}, completion: { _ in
self.animateImageViews()
})
}
}
let viewcontroller = Demo()
viewcontroller.view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
PlaygroundPage.current.liveView = viewcontroller.view

How to implement SwipeView?

I want to swipe each image to switch to another image like gallery app. I am now using this https://github.com/nicklockwood/SwipeView, but I don't know how to implement it. Should I drag a collection view inside my PhotoDetailViewController, or I only use it in coding. May anyone help me with this.
Here is my code:
import Foundation
import UIKit
import AAShareBubbles
import SwipeView
class PhotoDetailViewController: UIViewController, AAShareBubblesDelegate, SwipeViewDataSource, SwipeViewDelegate {
#IBOutlet var topView: UIView!
#IBOutlet var bottomView: UIView!
#IBOutlet var photoImageView: UIImageView!
var photoImage = UIImage()
var checkTapGestureRecognize = true
var swipeView: SwipeView = SwipeView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height))
override func viewDidLoad() {
title = "Photo Detail"
super.viewDidLoad()
photoImageView.image = photoImage
swipeView.dataSource = self
swipeView.delegate = self
let swipe = UISwipeGestureRecognizer(target: self, action: "swipeMethod")
swipeView.addGestureRecognizer(swipe)
swipeView.addSubview(photoImageView)
swipeView.pagingEnabled = false
swipeView.wrapEnabled = true
}
func swipeView(swipeView: SwipeView!, viewForItemAtIndex index: Int, reusingView view: UIView!) -> UIView! {
return photoImageView
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("SwipeCell", forIndexPath: indexPath) as! SwipeViewPhotoCell
return cell
}
#IBAction func onBackClicked(sender: AnyObject) {
self.navigationController?.popViewControllerAnimated(true)
}
#IBAction func onTabGestureRecognize(sender: UITapGestureRecognizer) {
print("on tap")
if checkTapGestureRecognize == true {
bottomView.hidden = true
topView.hidden = true
self.navigationController?.navigationBarHidden = true
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
photoImageView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
checkTapGestureRecognize = false
showAminationOnAdvert()
}
else if checkTapGestureRecognize == false {
bottomView.hidden = false
topView.hidden = false
self.navigationController?.navigationBarHidden = false
checkTapGestureRecognize = true
}
}
func showAminationOnAdvert() {
let transitionAnimation = CATransition();
transitionAnimation.type = kCAEmitterBehaviorValueOverLife
transitionAnimation.subtype = kCAEmitterBehaviorValueOverLife
transitionAnimation.duration = 2.5
transitionAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transitionAnimation.fillMode = kCAFillModeBoth
photoImageView.layer.addAnimation(transitionAnimation, forKey: "fadeAnimation")
}
#IBAction func onShareTouched(sender: AnyObject) {
print("share")
let myShare = "I am feeling *** today"
let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)
self.presentViewController(shareVC, animated: true, completion: nil)
// print("share bubles")
// let shareBubles: AAShareBubbles = AAShareBubbles.init(centeredInWindowWithRadius: 100)
// shareBubles.delegate = self
// shareBubles.bubbleRadius = 40
// shareBubles.sizeToFit()
// //shareBubles.showFacebookBubble = true
// shareBubles.showTwitterBubble = true
// shareBubles.addCustomButtonWithIcon(UIImage(named: "twitter"), backgroundColor: UIColor.whiteColor(), andButtonId: 100)
// shareBubles.show()
}
#IBAction func playAutomaticPhotoImages(sender: AnyObject) {
animateImages(0)
}
func animateImages(no: Int) {
var number: Int = no
if number == images.count - 1 {
number = 0
}
let name: String = images[number]
self.photoImageView!.alpha = 0.5
self.photoImageView!.image = UIImage(named: name)
//code to animate bg with delay 2 and after completion it recursively calling animateImage method
UIView.animateWithDuration(2.0, delay: 0.8, options:UIViewAnimationOptions.CurveEaseInOut, animations: {() in
self.photoImageView!.alpha = 1.0;
},
completion: {(Bool) in
number++;
self.animateImages(number);
print(String(images[number]))
})
}
}
Just drag and drop a UIView to your storyboard/XIB, and set its customclass to SwipeView.
Also set the delegate and datasource to the view controller which includes the UIView you just dragged.
Then in the viewcontroller, implement the required delegate methods similar to how you'd implement the methods for a tableview.

Resources