I want to realize a sort of matrix editable by touch.
A series of black or white cell that if tapped switch form black to withe or viceversa.
For doing this i will use UIImageView arranged in stack view a Tap Gesture Recognizer. But how do I know which UIImageView was tapped? And in which way I can change the UIImage in the UIImageView?
If they are just white and black, it would be much simpler to use a UIView and set its backgroundColor to .white or .black. You can use the tag property of the UIViews to identify them.
In your gestureRecognizer handler, the recognizer.view tells you the view that triggered the gesture.
You can assign the tags in Interface Builder. For example, if you have an 8x8 array of squares, you could use a 2-digit number where the first digit is the row and the second digit is the column. Thus, your tags would be 11, 12, ..., 87, 88.
func squareTapped(recognizer: UIGestureRecognizer) {
if let view = recognizer.view {
let tag = view.tag
let row = tag / 10
let col = tag % 10
print("The view at row \(row), column \(col) was tapped")
if view.backgroundColor == .black {
view.backgroundColor = .white
} else {
view.backgroundColor = .black
}
}
}
If you do want to use images, then load the images as properties of your viewController and assign them based upon the row and column of your image. Here I have used an Outlet Collection to hold all of the UIImageViews. In Interface Builder, you'd connect each of your cells to the squares property.
class BoardViewController: UIViewController {
let blackImage = UIImage(named: "blackImage")!
let whiteImage = UIImage(named: "whiteImage")!
#IBOutlet var squares: [UIImageView]!
var recognizersAdded = false
func setUpBoard() {
for imageview in squares {
if !recognizersAdded {
let recognizer = UITapGestureRecognizer(target: self, action: #selector(squareTapped))
imageview.addGestureRecognizer(recognizer)
imageview.isUserInteractionEnabled = true
}
let tag = view.tag
let row = tag / 10
let col = tag % 10
// Just for demo purposes, set up a checkerboard pattern
if (row + col) % 2 == 0 {
imageview.image = blackImage
} else {
imageview.image = whiteImage
}
}
recognizersAdded = true
}
func squareTapped(recognizer: UIGestureRecognizer) {
if let view = recognizer.view as? UIImageView {
let tag = view.tag
let row = tag / 10
let col = tag % 10
print("The view at row \(row), column \(col) was tapped")
if view.image == blackImage {
view.image = whiteImage
} else {
view.image = blackImage
}
}
}
}
The code I'm using now is this, it works well.
But I want to use a pan gesture (UIPanGetureRecognizer) to change colors of the UIView. How can I do?
class ViewController: UIViewController {
#IBOutlet weak var PrincipalRowStack: UIStackView!
let rowsNumber=10
let colsNumber=10
override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
var rowsStack = [UIStackView]()
var images = [[UIView]]()
var gestRec = [[UITapGestureRecognizer]]()
for r in 0...rowsNumber-1 {
rowsStack.append(UIStackView())
rowsStack[r].axis = .horizontal
rowsStack[r].distribution = .fillEqually
images.append([UIView]())
gestRec.append([UITapGestureRecognizer]())
for c in 0...colsNumber-1{
images[r].append(UIView())
gestRec[r].append(UITapGestureRecognizer())
gestRec[r][c].addTarget(self, action: #selector(ViewController.Tap(_:)))
images[r][c].contentMode = .scaleToFill
images[r][c].layer.borderWidth = 1
images[r][c].layer.borderColor = UIColor(red:0.5, green:0.5, blue:0.5, alpha: 1.0).cgColor
images[r][c].addGestureRecognizer(gestRec[r][c])
rowsStack[r].addArrangedSubview(images[r][c])
}
}
for s in rowsStack{
PrincipalRowStack.addArrangedSubview(s)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Tap(_ recognizer: UITapGestureRecognizer) {
if let view = recognizer.view {
//let tag = view.tag
//let row = tag / 10
//let col = tag % 10
print("Tapped!")
if view.backgroundColor == .black {
view.backgroundColor = .white
} else {
view.backgroundColor = .black
}
}
}
}
I'solved adding a PanGestureRecognizer to the principalStackView and than i use this function with a hitTest to know which view should change color.
func pan(_ recognizer: UIPanGestureRecognizer){
if (recognizer.state == .began ||
recognizer.state == .changed)
{
let loc = recognizer.location(in: principalRowStack)
let view = principalRowStack.hitTest(loc, with: UIEvent())
view?.backgroundColor = .black
}
}
Assign a tag to each UIImageView and Add Tap gestures on them.
Upon tap on any view , you can get the image view tapped by :
UIImageView *imageView = gestureRecogniser.view
Related
Looking to add a tap gesture to an array of UIViews - without success. Tap seems not to be recognised at this stage.
In the code (extract) below:
Have a series of PlayingCardViews (each a UIView) showing on the main view.
Brought together as an array: cardView.
Need to be able to tap each PlayingCardView independently (and then to be able to identify which one was tapped).
#IBOutlet private var cardView: [PlayingCardView]!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(tapCard(sender: )))
for index in cardView.indices {
cardView[index].isUserInteractionEnabled = true
cardView[index].addGestureRecognizer(tap)
cardView[index].tag = index
}
}
#objc func tapCard (sender: UITapGestureRecognizer) {
if sender.state == .ended {
let cardNumber = sender.view.tag
print("View tapped !")
}
}
You need
#objc func tapCard (sender: UITapGestureRecognizer) {
let clickedView = cardView[sender.view!.tag]
print("View tapped !" , clickedView )
}
No need to check state here as the method with this gesture type is called only once , also every view should have a separate tap so create it inside the for - loop
for index in cardView.indices {
let tap = UITapGestureRecognizer(target: self, action: #selector(tapCard(sender: )))
I will not recommend the selected answer. Because creating an array of tapGesture doesn't make sense to me in the loop. Better to add gesture within PlaycardView.
Instead, such layout should be designed using UICollectionView. If in case you need to custom layout and you wanted to use scrollView or even UIView, then the better approach is to create single Gesture Recognizer and add to the superview.
Using tap gesture, you can get the location of tap and then you can get the selectedView using that location.
Please refer to below example:
import UIKit
class PlayCardView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.red
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
backgroundColor = UIColor.red
}
}
class SingleTapGestureForMultiView: UIViewController {
var viewArray: [UIView]!
var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView = UIScrollView(frame: UIScreen.main.bounds)
view.addSubview(scrollView)
let tapGesture = UITapGestureRecognizer(target: self,
action: #selector(tapGetsure(_:)))
scrollView.addGestureRecognizer(tapGesture)
addSubviews()
}
func addSubviews() {
var subView: PlayCardView
let width = UIScreen.main.bounds.width;
let height = UIScreen.main.bounds.height;
let spacing: CGFloat = 8.0
let noOfViewsInARow = 3
let viewWidth = (width - (CGFloat(noOfViewsInARow+1) * spacing))/CGFloat(noOfViewsInARow)
let viewHeight = (height - (CGFloat(noOfViewsInARow+1) * spacing))/CGFloat(noOfViewsInARow)
var yCordinate = spacing
var xCordinate = spacing
for index in 0..<20 {
subView = PlayCardView(frame: CGRect(x: xCordinate, y: yCordinate, width: viewWidth, height: viewHeight))
subView.tag = index
xCordinate += viewWidth + spacing
if xCordinate > width {
xCordinate = spacing
yCordinate += viewHeight + spacing
}
scrollView.addSubview(subView)
}
scrollView.contentSize = CGSize(width: width, height: yCordinate)
}
#objc
func tapGetsure(_ gesture: UITapGestureRecognizer) {
let location = gesture.location(in: scrollView)
print("location = \(location)")
var locationInView = CGPoint.zero
let subViews = scrollView.subviews
for subView in subViews {
//check if it subclass of PlayCardView
locationInView = subView.convert(location, from: scrollView)
if subView.isKind(of: PlayCardView.self) {
if subView.point(inside: locationInView, with: nil) {
// this view contains that point
print("Subview at \(subView.tag) tapped");
break;
}
}
}
}
}
You can try to pass the view controller as parameter to the views so they can call a function on parent view controller from the view. To reduce memory you can use protocols. e.x
protocol testViewControllerDelegate: class {
func viewTapped(view: UIView)
}
class testClass: testViewControllerDelegate {
#IBOutlet private var cardView: [PlayingCardView]!
override func viewDidLoad() {
super.viewDidLoad()
for cardView in self.cardView {
cardView.fatherVC = self
}
}
func viewTapped(view: UIView) {
// the view that tapped is passed ass parameter
}
}
class PlayingCardView: UIView {
weak var fatherVC: testViewControllerDelegate?
override func awakeFromNib() {
super.awakeFromNib()
let gr = UITapGestureRecognizer(target: self, action: #selector(self.viewDidTap))
self.addGestureRecognizer(gr)
}
#objc func viewDidTap() {
fatherVC?.viewTapped(view: self)
}
}
I want to realize a sort of matrix editable by touch.
A series of black or white cell that if tapped switch form black to withe or viceversa.
For doing this i will use UIImageView arranged in stack view a Tap Gesture Recognizer. But how do I know which UIImageView was tapped? And in which way I can change the UIImage in the UIImageView?
If they are just white and black, it would be much simpler to use a UIView and set its backgroundColor to .white or .black. You can use the tag property of the UIViews to identify them.
In your gestureRecognizer handler, the recognizer.view tells you the view that triggered the gesture.
You can assign the tags in Interface Builder. For example, if you have an 8x8 array of squares, you could use a 2-digit number where the first digit is the row and the second digit is the column. Thus, your tags would be 11, 12, ..., 87, 88.
func squareTapped(recognizer: UIGestureRecognizer) {
if let view = recognizer.view {
let tag = view.tag
let row = tag / 10
let col = tag % 10
print("The view at row \(row), column \(col) was tapped")
if view.backgroundColor == .black {
view.backgroundColor = .white
} else {
view.backgroundColor = .black
}
}
}
If you do want to use images, then load the images as properties of your viewController and assign them based upon the row and column of your image. Here I have used an Outlet Collection to hold all of the UIImageViews. In Interface Builder, you'd connect each of your cells to the squares property.
class BoardViewController: UIViewController {
let blackImage = UIImage(named: "blackImage")!
let whiteImage = UIImage(named: "whiteImage")!
#IBOutlet var squares: [UIImageView]!
var recognizersAdded = false
func setUpBoard() {
for imageview in squares {
if !recognizersAdded {
let recognizer = UITapGestureRecognizer(target: self, action: #selector(squareTapped))
imageview.addGestureRecognizer(recognizer)
imageview.isUserInteractionEnabled = true
}
let tag = view.tag
let row = tag / 10
let col = tag % 10
// Just for demo purposes, set up a checkerboard pattern
if (row + col) % 2 == 0 {
imageview.image = blackImage
} else {
imageview.image = whiteImage
}
}
recognizersAdded = true
}
func squareTapped(recognizer: UIGestureRecognizer) {
if let view = recognizer.view as? UIImageView {
let tag = view.tag
let row = tag / 10
let col = tag % 10
print("The view at row \(row), column \(col) was tapped")
if view.image == blackImage {
view.image = whiteImage
} else {
view.image = blackImage
}
}
}
}
The code I'm using now is this, it works well.
But I want to use a pan gesture (UIPanGetureRecognizer) to change colors of the UIView. How can I do?
class ViewController: UIViewController {
#IBOutlet weak var PrincipalRowStack: UIStackView!
let rowsNumber=10
let colsNumber=10
override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
var rowsStack = [UIStackView]()
var images = [[UIView]]()
var gestRec = [[UITapGestureRecognizer]]()
for r in 0...rowsNumber-1 {
rowsStack.append(UIStackView())
rowsStack[r].axis = .horizontal
rowsStack[r].distribution = .fillEqually
images.append([UIView]())
gestRec.append([UITapGestureRecognizer]())
for c in 0...colsNumber-1{
images[r].append(UIView())
gestRec[r].append(UITapGestureRecognizer())
gestRec[r][c].addTarget(self, action: #selector(ViewController.Tap(_:)))
images[r][c].contentMode = .scaleToFill
images[r][c].layer.borderWidth = 1
images[r][c].layer.borderColor = UIColor(red:0.5, green:0.5, blue:0.5, alpha: 1.0).cgColor
images[r][c].addGestureRecognizer(gestRec[r][c])
rowsStack[r].addArrangedSubview(images[r][c])
}
}
for s in rowsStack{
PrincipalRowStack.addArrangedSubview(s)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func Tap(_ recognizer: UITapGestureRecognizer) {
if let view = recognizer.view {
//let tag = view.tag
//let row = tag / 10
//let col = tag % 10
print("Tapped!")
if view.backgroundColor == .black {
view.backgroundColor = .white
} else {
view.backgroundColor = .black
}
}
}
}
I'solved adding a PanGestureRecognizer to the principalStackView and than i use this function with a hitTest to know which view should change color.
func pan(_ recognizer: UIPanGestureRecognizer){
if (recognizer.state == .began ||
recognizer.state == .changed)
{
let loc = recognizer.location(in: principalRowStack)
let view = principalRowStack.hitTest(loc, with: UIEvent())
view?.backgroundColor = .black
}
}
Assign a tag to each UIImageView and Add Tap gestures on them.
Upon tap on any view , you can get the image view tapped by :
UIImageView *imageView = gestureRecogniser.view
I have an array of images if I pressed the button the image will randomly appear. and there is an image down also called from the array but it appears randomly just once I opened the game. I want to do a conditions statement for button pressed. like I have 5 conditions once the button clicked: 1- if pressed and the image appeared is not same as the UIIMage view down score will be added
2- if button did not press for 2 seconds will appear down.
3- if button pressed and it is same as the UIimage so game over.
4- If he calculated 4 images down because he didn't hit the image in 2 seconds he will lose.
var array:[UIImage] = [UIImage(named: "1.png")!,
UIImage(named: "2.png")!,
UIImage(named: "3.png")!,
UIImage(named: "4.png")!,
UIImage(named: "5.png")!,
UIImage(named: "6.png")!,
UIImage(named: "7.png")!,
UIImage(named: "8.png")!,
UIImage(named: "9.png")!,
UIImage(named: "10.png")!]
var random = arc4random_uniform(10)
#IBAction func myButtonPressed(button: UIButton) {
var randomNum: UInt32 = 10
randomNum = arc4random_uniform(UInt32(array.count))
myButton.setImage(UIImage(named: "bird\(randomNum).png"), for: UIControlState.normal)
// myButton.setImage(UIImage(named: "\(randomNum).png"), for: UI)
self.myImage.animationImages = array
let buttonWidth = myButton.frame.width
let buttonHeight = myButton.frame.height
// Find the width and height of the enclosing view
let viewWidth = myButton.superview!.bounds.width
let viewHeight = myButton.superview!.bounds.height
// Compute width and height of the area to contain the button's center
let xwidth = viewWidth - buttonWidth
let yheight = viewHeight - buttonHeight
// Generate a random x and y offset
let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))
// Offset the button's center by the random offsets.
myButton.center.x = xoffset + buttonWidth / 2
myButton.center.y = yoffset + buttonHeight / 2
/* for i in array{
if myButton != myImage{
randomNum = arc4random_uniform(UInt32(array.count))
}
if else
} */
}
Perhaps you could try something like this. As for your first condition, what is your UIImage View Variable. Is it another UIImage View that is being displayed on the storyboard?
import UIKit
class ViewController: UIViewController {
var array: [UIImage] = [UIImage(named: "1.png")!,
UIImage(named: "2.png")!,
UIImage(named: "3.png")!,
UIImage(named: "4.png")!,
UIImage(named: "5.png")!]
#IBOutlet weak var myButton: UIButton!
var pressed: Bool = false;
var score: Int = 0;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//2nd Condition: if button did not press for 2 seconds will appear down
if(pressed == false){
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(ViewController.doSomething), userInfo: nil, repeats: true)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func isPressed(_ sender: UIButton) {
pressed = true;
var randomNum: UInt32 = 10
randomNum = arc4random_uniform(UInt32(array.count));
myButton.setImage(UIImage(named: "\(randomNum).png"), for: .normal)
// 1st Condition: if pressed and the image appeared is not same as the UIIMage view down score will be added
if( UIImage(named: "\(randomNum).png") != myButton.currentImage){
//Add View Down Score
score += 1;
}
// 3rd Condition: if button pressed and it is same as the UIimage so game over
else{
// Game Over
}
}
func doSomething() {
// Not sure what you mean by will appear down
print("Action")
}
}
I am not quite sure what your 4th condition does. Please clarify
Intro:
Functionality of adding multiple stickers/emoji to a view.
Setup:
There are 2 view controllers - one to which we're adding stickers, and another with a collectionview of stickers.
Stickers themselves are passed in arrays in 'prepareForSegue' func.
There are 2 arrays, one with just sticker images, another - with UIImageViews - stickers that were already panned, pinched and rotated.
Bug:
After adding 2nd sticker, the AddingStickersVC reappears but previous sticker isn't where we left if. It is pinched and zoomed, but not panned. Also new stickers are sticked to the first one (same frame?).
We can pinch and zoom previous stickers separately from new ones - (nope, they have their own frames), but we can't separate them.
End up having a stack of UIImageViews that takes rotating and pinching separately but pans all together.
Also, speed of panning is increasing after each additional sticker (the panning gesture is added multiple time?).
Hierarchy of views
Stickers are added to 'viewForEmoji' view (viewForImgAndEmoji).
AddingStickersVC:
#IBOutlet weak var viewForImgAndEmoji: UIView!
#IBOutlet weak var mainImg: UIImageView!
#IBOutlet weak var viewForSnapshot: UIView!
var imageData: Data!
var imageItself: UIImage!
var currentUserPostRef: FIRDatabaseReference!
var emojiImage: UIImage!
var geoFire: GeoFire!
var arrayOfEmojis = [UIImage]()
var arrayOfEmojiViews = [UIImageView]()
var n:Int = 1
override func viewDidLoad() {
super.viewDidLoad()
if imageData != nil {
let img = UIImage(data: imageData)
let fixedImg = img!.fixOrientation(img: img!)
mainImg.image = fixedImg
} else if imageItself != nil {
mainImg.image = imageItself
}
if arrayOfEmojiViews.count != 0 {
for emojiView1 in arrayOfEmojiViews {
viewForImgAndEmoji.addSubview(emojiView1)
}
}
// get image out of array.
if arrayOfEmojis.count != 0 {
for emoji in arrayOfEmojis {
let emojiView = UIImageView(image: emoji)
emojiView.tag = n
emojiView.frame = CGRect(x: 153, y: 299, width: 70, height: 70)
emojiView.isUserInteractionEnabled = true
let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
pan.delegate = self
viewForImgAndEmoji.addGestureRecognizer(pan)
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch(recognizer:)))
pinch.delegate = self
viewForImgAndEmoji.addGestureRecognizer(pinch)
let rotate = UIRotationGestureRecognizer(target: self, action: #selector(self.handleRotate(recognizer:)))
rotate.delegate = self
viewForImgAndEmoji.addGestureRecognizer(rotate)
// check so we won't add previous emoji. only new.
if viewForImgAndEmoji.viewWithTag(n) == nil {
viewForImgAndEmoji.addSubview(emojiView)
}
n += 1
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if arrayOfEmojis.count != 0 {
for j in 1...n {
if var view1 = self.viewForImgAndEmoji.viewWithTag(j) as? UIImageView {
arrayOfEmojiViews.append(view1)
print("Zhenya: views frame is \(view1.frame)")
}
}
}
if segue.identifier == "EmojiCollectionVC" {
if let emojiCollection = segue.destination as? EmojiCollectionVC{
if let image = sender as? UIImage {
emojiCollection.userImage = image
if arrayOfEmojis.count != 0 {
//arrayToStoreEmojis
emojiCollection.arrayToStoreEmojis = arrayOfEmojis
emojiCollection.arrayToStoreEmojiViews = arrayOfEmojiViews
}
}
}
}
}
#IBAction func handlePan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.viewForImgAndEmoji)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.viewForImgAndEmoji)
}
#IBAction func handlePinch(recognizer: UIPinchGestureRecognizer) {
let pinchPoint = recognizer.location(in: viewForImgAndEmoji)
let ourEmojiView = viewForImgAndEmoji.hitTest(pinchPoint, with: nil)
ourEmojiView!.transform = ourEmojiView!.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
recognizer.scale = 1
}
#IBAction func handleRotate(recognizer: UIRotationGestureRecognizer){
let rotatePoint = recognizer.location(in: viewForImgAndEmoji)
let ourEmojiView = viewForImgAndEmoji.hitTest(rotatePoint, with: nil)
ourEmojiView!.transform = ourEmojiView!.transform.rotated(by: recognizer.rotation)
recognizer.rotation = 0
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
And EmojiCollectionVC:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! EmojiCollectionCell
let chosenEmoji = cell.emojiView.image as UIImage!
arrayToStoreEmojis.append(chosenEmoji!)
performSegue(withIdentifier: "backToEmojiVC", sender: arrayToStoreEmojis)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "backToEmojiVC"{
if let destinationVC = segue.destination as? EmojiVC {
if let array = sender as? [UIImage] {
destinationVC.arrayOfEmojis = arrayToStoreEmojis
destinationVC.arrayOfEmojiViews = arrayToStoreEmojiViews
let data = UIImagePNGRepresentation(userImage)
destinationVC.imageData = data
}
}
}
}
Found solution.
There reason why all new imageViews were stacked - because after panning imageViews didn't change their location in view. The whole f view was moving.
(to find this, spent 8hours tracking changes of frame origins with 'prints' at every step of the program).
And the reason the whole view was moving - because panning gesture was added to the whole view.
So instead of
let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
pan.delegate = self
viewForImgAndEmoji.addGestureRecognizer(pan)
i needed:
let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
pan.delegate = self
emojiView.addGestureRecognizer(pan)
What is interesting, for pinching and rotating - we still add them to the whole view that contains emojiView:
viewForImgAndEmoji.addGestureRecognizer(pinch)
...
viewForImgAndEmoji.addGestureRecognizer(rotate)
I'm trying to change the indicator image , following the example in EAIntroView
here is the objective-c code from EAIntroView
SMPageControl *pageControl = [[SMPageControl alloc] init];
pageControl.pageIndicatorImage = [UIImage imageNamed:#"pageDot"];
pageControl.currentPageIndicatorImage = [UIImage imageNamed:#"selectedPageDot"];
[pageControl sizeToFit];
intro.pageControl = (UIPageControl *)pageControl;
intro.pageControlY = 130.f;
and here is the swift code I'm trying to implement
// SMPageControl
pageControl = SMPageControl()
pageControl.pageIndicatorImage = UIImage(named: "pageDot")
pageControl.currentPageIndicatorImage = UIImage(named: "selectedPageDot")
pageControl.sizeToFit()
pageControl.backgroundColor = UIColor.clearColor()
intro.pageControl = pageControl as? UIPageControl
swift code has a warning here
intro.pageControl = pageControl as? UIPageControl
the warning is :
Cast from 'SMPageControl!' to unrelated type 'UIPageControl' always fails
any help ?
For changing page Indicator Image [SMPageControl] / EAIntroView in swift I have created custom class of UIPageControl like below:
import UIKit
class CustomPageControl: UIPageControl {
let activePage: UIImage = UIImage(named: "icon-selected-dot")!
let inActivePage: UIImage = UIImage(named: "icon-dot")!
override var numberOfPages: Int {
didSet {
updateDots()
}
}
override var currentPage: Int {
didSet {
updateDots()
}
}
override func awakeFromNib() {
super.awakeFromNib()
self.pageIndicatorTintColor = UIColor.clear
self.currentPageIndicatorTintColor = UIColor.clear
self.clipsToBounds = false
}
func updateDots() {
var i = 0
for view in self.subviews {
var imageView = self.imageView(forSubview: view)
if imageView == nil {
if i == self.currentPage {
imageView = UIImageView(image: activePage)
} else {
imageView = UIImageView(image: inActivePage)
}
imageView!.center = view.center
view.addSubview(imageView!)
view.clipsToBounds = false
}
if i == self.currentPage {
imageView!.alpha = 1.0
imageView?.image = activePage
} else {
imageView!.alpha = 0.5
imageView?.image = inActivePage
}
i += 1
}
}
fileprivate func imageView(forSubview view: UIView) -> UIImageView? {
var dot: UIImageView?
if let dotImageView = view as? UIImageView {
dot = dotImageView
} else {
for foundView in view.subviews {
if let imageView = foundView as? UIImageView {
dot = imageView
break
}
}
}
return dot
}
}
then in your class just add these lines and you can use custom images for dot
//Custom page Control
let pageControl = CustomPageControl()
pageControl.updateDots()
intro.pageControl = pageControl
Hope it will work for you! #ynamao
You can see from the source code of SMPageControl that it isn't a subclass of UIPageControl. Which means the error is expected: UIPageControl is a completely unrelated type, to which the value cannot be cast.
The Objective-C you pointed to might work, but it's bad and wrong: inline cast to UIPageControl achieves nothing here and can cause internal inconsistencies.
This is exactly the kind of sloppiness that Swift compiler is designed to prevent, and it's doing its job well.
Your best bet is to forgo using this library in Swift code.