I am having main ScrollView with pages Enable..Under that I have Subscrollviews and ImageView to display images..I want to Delete Selected image from main scrollview..I don't have any idea to how to get that selected imageView from scrollview and remove it...Here is my code...
func createScroll()
{
var arrayImage = NSUserDefaults.standardUserDefaults().objectForKey("ImageArray") as NSMutableArray
var index = NSUserDefaults.standardUserDefaults().objectForKey("Index") as Int
var scroll = UIScrollView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
var page = UIPageControl(frame: CGRectMake(0, 0, scroll.frame.size.width, scroll.frame.size.height))
page.numberOfPages = arrayImage.count
page.currentPage = 0
page.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
scroll.contentSize = CGSizeMake(scroll.frame.size.width * CGFloat(arrayImage.count - 1) , 0)
scroll.delegate = self
scroll.pagingEnabled = true
scroll.maximumZoomScale = 6
scroll.minimumZoomScale = 0.5
scroll.tag = Tags.Tag_MainScroll.rawValue
scroll.addSubview(page)
view.addSubview(scroll)
scroll.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
scroll.contentOffset = CGPointMake(scroll.frame.size.width * CGFloat(index - 1) , 0)
}
//MARK: Display Selected Image
func displayImage()
{
var mainScroll : UIScrollView = view.viewWithTag(Tags.Tag_MainScroll.rawValue) as UIScrollView
var arrayImage = NSUserDefaults.standardUserDefaults().objectForKey("ImageArray") as NSMutableArray
var xPosition : CGFloat = 0
for i in 1...22
{
var subScroll = UIScrollView(frame: CGRectMake(xPosition, 0, view.frame.size.width, view.frame.size.height))
subScroll.delegate = self
subScroll.maximumZoomScale = 6
subScroll.minimumZoomScale = 0.5
subScroll.tag = Tags.Tag_SubScroll.rawValue + i
mainScroll.addSubview(subScroll)
var imgToDisplay : UIImage = UIImage(named: "\(arrayImage[i])")! as UIImage
var imgView : UIImageView = UIImageView(frame: CGRectMake((subScroll.frame.size.width - 100 ) / 2,(subScroll.frame.size.height - 100) / 2, 100, 100))
subScroll.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height)
imgView.image = imgToDisplay
imgView.tag = Tags.Tag_imgView.rawValue
subScroll.addSubview(imgView)
xPosition += subScroll.frame.size.width
}
}
Here Is my Delete Function...
func Delete()
{
var imgView : UIImageView? = view.viewWithTag(Tags.Tag_imgView.rawValue) as? UIImageView
var scroll : UIScrollView = view.viewWithTag(Tags.Tag_MainScroll.rawValue) as UIScrollView
for img in scroll.subviews
{
imgView = img as? UIImageView
imgView?.removeFromSuperview()
}
}`
have u tryed this one..
let subViews = self.scrollView.subviews
for subview in subViews{
subview.removeFromSuperview()
}
HERE is the reference
as per my code this Delete Function is working for me
func Delete()
{
var imgView : UIImageView? = view.viewWithTag(Tags.Tag_imgView.rawValue) as? UIImageView
var scroll : UIScrollView = view.viewWithTag(Tags.Tag_MainScroll.rawValue) as UIScrollView
var number : Int = Int(scroll.contentOffset.x / scroll.frame.size.width)
var page : UIPageControl = view.viewWithTag(Tags.Tag_page.rawValue) as UIPageControl
page.currentPage = number
println("\(page.currentPage)")
if var array : NSArray = NSUserDefaults.standardUserDefaults().objectForKey("ImageArray") as? NSArray
{
arrayImage = array.mutableCopy() as NSMutableArray
}
page.removeFromSuperview()
number++
arrayImage.removeObjectAtIndex(number)
NSUserDefaults.standardUserDefaults() .setObject(arrayImage, forKey: "ImageArray")
println("\(arrayImage[number])")
println("\(arrayImage)")
NSUserDefaults.standardUserDefaults().synchronize()
displayImage()
}
Related
On my users profile page, there is a scrollview users slide across to see more photos. Everything works with adding the photos but the problem is that each image added to the scrollviews subview, is added twice. This is the code I am using to populate the scrollview. I currently have an Imageview embed inside the scrollview on storyboard and use that as the default if user has no photos. The at the bottom should show the bug. I have tried using a combination of clipstobounds, scaleaspectfit, and setting background color to clear but nothing stops from showing an additional image in the background. Any ideas would be appreciated. On a side note, the bug is only noticed on the iphone 8 plus
var pages: Int = 1
var numPhotos: CGFloat = 1
var position: CGFloat = 1
let scrollWidth: CGFloat = profileImage.frame.width
let scrollHeight: CGFloat = profileImage.frame.height
let scrollY: CGFloat = profileImage.frame.origin.y
if let profileImg = user.profileImage {
profileResource = ImageResource(downloadURL: URL(string: profileImg)!, cacheKey: profileImg)
profileImage.kf.setImage(with: profileResource)
} else {
profileImage.image = #imageLiteral(resourceName: "requests_icon")
}
nameLbl.text = user.fullName
influenceLbl.text = "\(user.score)"
followersLbl.text = "\(user.followers)"
followingLbl.text = "\(user.following)"
if let image1 = user.photo1 {
let imageOne = UIImageView(frame: CGRect(x: scrollWidth * position, y: scrollY, width: scrollWidth, height: scrollHeight))
imageOne.contentMode = .scaleAspectFit
photo1Resource = ImageResource(downloadURL: URL(string: image1)!, cacheKey: image1)
imageOne.kf.setImage(with: photo1Resource)
pages = pages + 1
numPhotos = numPhotos + 1
position = position + 1
scrollView.addSubview(imageOne)
} else {
}
if let image2 = user.photo2 {
let imageTwo = UIImageView(frame: CGRect(x: scrollWidth * position, y: scrollY, width: scrollWidth, height: scrollHeight))
imageTwo.contentMode = .scaleAspectFit
photo2Resource = ImageResource(downloadURL: URL(string: image2)!, cacheKey: image2)
imageTwo.kf.setImage(with: photo2Resource)
pages = pages + 1
numPhotos = numPhotos + 1
position = position + 1
scrollView.addSubview(imageTwo)
} else {
}
if let image3 = user.photo3 {
let imageThree = UIImageView(frame: CGRect(x: scrollWidth * position, y: scrollY, width: scrollWidth, height: scrollHeight))
imageThree.contentMode = .scaleAspectFit
photo3Resource = ImageResource(downloadURL: URL(string: image3)!, cacheKey: image3)
imageThree.kf.setImage(with: photo3Resource)
pages = pages + 1
numPhotos = numPhotos + 1
position = position + 1
scrollView.addSubview(imageThree)
} else {
}
scrollView.contentSize = CGSize(width: profileImage.frame.width * numPhotos, height: scrollHeight)
scrollView.delegate = self
scrollView.contentMode = .scaleAspectFit
scrollView.clipsToBounds = true
pageControl.numberOfPages = pages
pageControl.currentPage = 0
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
let pageWidth:CGFloat = scrollView.frame.width
let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth) + 1
self.pageControl.currentPage = Int(currentPage)
}
I found how to calculate dynamic uiwebview height according to its content. My code is :
println("webViweDidFinish started")
var frame:CGRect = myWebView.frame
frame.size.height = 1
var fittingSize :CGSize = myWebView.sizeThatFits(CGSizeZero)
frame.size = fittingSize
myWebView.frame = frame
var contentsizeHeight = webView.scrollView.contentSize.height
var yCoordinateOfWebView = myWebView.frame.origin.y
var contentHeight: CGFloat = webView.scrollView.contentSize.height;
myScrollView.contentSize = CGSizeMake(self.view.frame.width, yCoordinateOfWebView + contentsizeHeight + labelAuthorOfArticle.frame.height) //
UIView.animateWithDuration(0.5, animations: {
self.heightConstraints.constant = fittingSize.height
self.myWebView.layoutIfNeeded()
self.myScrollContentView.layoutIfNeeded()
})
self.activityIndicator.stopAnimating()
If I use this approach:
var height:NSString! = webView.stringByEvaluatingJavaScriptFromString("document.height;")
var heightInFloat = height.floatValue
How to convert height to CGFloat? I can only convert it to float as you see.
The problem is that when webview has too many images the height is calculated wrong. Oppositely when there is no image height is calculated right. When I reload the function, the second time it calculates right (with images). Here is content of string that I am loading:
class func formatContentText(inout text: NSString)-> NSString{
text = text.stringByReplacingOccurrencesOfString("src=\"", withString: "src=\"http://dixinews.kz")
var deviceWidth = "300"
text = "<html><head><meta name = \"viewport\" content = \"user-scalable=no, width=" + deviceWidth + " , maximum-scale=1.0\"><style>img{max-width:100%%;height:auto !important;width:auto !important;};</style></head><body>" + text + "</body></html>"
return text
}
I used approach with javascript. Here is my code:
func webViewDidFinishLoad(webView: UIWebView) {
var frame:CGRect = myWebView.frame
// javascript
var height:NSString! = webView.stringByEvaluatingJavaScriptFromString("document.height;")
var heightInFloat = height.floatValue // convert to float
var heightINCGFloat = CGFloat(heightInFloat) convert to cgfloat
frame.size.height = heightINCGFloat //set heigh
myWebView.frame = frame // set frame
myWebView.scrollView.contentSize.height = myWebView.frame.height
var contentsizeHeight = webView.scrollView.contentSize.height
var yCoordinateOfWebView = myWebView.frame.origin.y
myScrollView.contentSize = CGSizeMake(self.view.frame.width, yCoordinateOfWebView + contentsizeHeight + labelAuthorOfArticle.frame.height) //
UIView.animateWithDuration(0.5, animations: {
self.heightConstraints.constant = heightINCGFloat
self.myWebView.layoutIfNeeded()
self.myScrollContentView.layoutIfNeeded()
})
self.activityIndicator.stopAnimating()
}
I am trying to scrolling image automatically in swift. I have tried below. I am trying to scroll through swift coding, instead of StoryBoard. It is scrolling well. But, Images are not adding to the image view. But, UIImageView bgcolour has been changed to green. My coding is below.
Kindly Guide me.
var str_1 : String = "one.jpg"
var str_2 : String = "two.jpg"
let img_1 : UIImage = UIImage(named: str_1)!
let img_2 : UIImage = UIImage(named: str_2)!
img_arr.addObject(img_1)
img_arr.addObject(img_2)
scroll_view = UIScrollView(frame: CGRectMake(0, 200, 320, 130))
for(var y : Int = 0; y < img_arr.count; y++)
{
//scr_img_vw = UIImageView(image: img_1)
//scr_img_vw.addSubview(UIImageView(image: img_arr.objectAtIndex(y) as UIImage))
scr_img_vw = UIImageView(image: img_arr.objectAtIndex(y) as UIImage)
//scr_img_vw.image = UIImage(named: img_arr.objectAtIndex(y) as NSString)
scr_img_vw = UIImageView(frame: CGRectMake(320 * CGFloat(y), 0, 320, 130))
scr_img_vw.backgroundColor = UIColor.greenColor()
self.scroll_view.addSubview(scr_img_vw)
}
self.view.addSubview(scroll_view)
scroll_view.contentSize = CGSizeMake(scroll_view.frame.size.width * CGFloat(img_arr.count), 130)
scroll_view.scrollEnabled = true
scroll_view.pagingEnabled = true
scroll_view.bounces = true
You are allocating image twice. Change the fore loop as follows:
for(var y : Int = 0; y < img_arr.count; y++)
{
scr_img_vw = UIImageView(image: img_arr.objectAtIndex(y) as UIImage)
scr_img_vw.frame = CGRectMake(320 * CGFloat(y), 0, 320, 130))
scr_img_vw.backgroundColor = UIColor.greenColor()
self.scroll_view.addSubview(scr_img_vw)
}
The better swift way would be like this:
let img1 = UIImage(named: "one.jpg")!
let img2 = UIImage(named: "two.jpg")!
var imageArray = [UIImage]()
imageArray.append(img1)
imageArray.append(img2)
for (index, image) in enumerate(imageArray) {
var imageView = UIImageView(image: image)
imageView.frame = CGRectMake(320 * CGFloat(index), 0, 320, 130))
self.scrollView.addSubview(imageView)
}
I am adding Tabbarcontroller in Main View, but i didn't set its background colour... I have already try tabBar.barTintColor but it's not working..
So i am trying to Add View as TabbarController (Which work like UITabbar) but i am not succeeded on it... Can any one please help me out.Here is the code
var tabBar = UITabBarController()
var frameOfView : CGRect = UIScreen.mainScreen().bounds
tabBar.view.frame = CGRectMake(frameOfView.origin.x, frameOfView.size.height, frameOfView.size.width, frameOfView.size.height-100)
var viewTabBar = UIView(frame: CGRectMake(frameOfView.origin.x, 430, frameOfView.size.width, 100))
var BtnHomeTab = UIButton(frame: CGRectMake(0, 0, 30, 30))
var imageHomeTab = UIImage(named: "home_selected.png")
viewTabBar.addSubview(BtnHomeTab)
have to hide UITabbarcontroller ..:)
and then Add this code to Your appDelegate.
//MARK: CREATE BOTTOMBAR BUTTON
func craeteBottonBarButton()
{
var viewBottom : UIView = UIView(frame: CGRectMake(0,window!.frame.size.height - 50.0,window!.frame.size.width,60.0))
viewBottom.tag = TagWindow.Tag_Bottom.rawValue
window!.addSubview(viewBottom)
window!.bringSubviewToFront(viewBottom)
viewBottom.backgroundColor = GlobalConstants.GlobalConstants.ButtonConstant.BUTTONBACKGROUNDCOLOR
viewBottom.tag = TagWindow.Tag_Bottom.rawValue
var x : CGFloat = 0.0
for var i = 0 ; i < 5 ; i++
{
var btn = createTabBarButton(viewBottom,
framebtn: CGRectMake(x,0, 65.0, 50.0),
tag: (i + 1),
imgName: arrayImage.objectAtIndex(i) as NSString , btnName: arrayLblName.objectAtIndex(i) as NSString) as UIButton
viewBottom.addSubview(btn)
x += 64.5
}
}
Give all navigation controller to the button
func createTabBar() -> UITabBarController
{
var storyBoard = UIStoryboard(name: "Main", bundle: nil)
tabBar.delegate = self
var tabBarFont = UITabBar()
let HomeTab = storyBoard.instantiateViewControllerWithIdentifier("HomeVC") as HomeVC
let navigation_home = UINavigationController(rootViewController: HomeTab)
navigation_home.navigationBarHidden = true
let SearchTab = storyBoard.instantiateViewControllerWithIdentifier("SearchVC") as SearchVC
let navigation_search = UINavigationController(rootViewController: SearchTab)
navigation_search.navigationBarHidden = true
let SettingTab = storyBoard.instantiateViewControllerWithIdentifier("SettingVC") as SettingVC
let navigation_Setting = UINavigationController(rootViewController: SettingTab)
navigation_Setting.navigationBarHidden = true
let MySeatTab = storyBoard.instantiateViewControllerWithIdentifier("MySeatVC") as MySeatVC
let navigation_MySeat = UINavigationController(rootViewController: MySeatTab)
navigation_MySeat.navigationBarHidden = true
I'm a new learner of ios programming. I have tried to search with another example and more questions at stackoverflow but it's not my goal. I want to set an image of dot at index 0 of UIPageControl as similar as iPhone search homescreen. Have any way to do it ? Please explain me with some code or another useful link.
Thanks in advance
I have found a solution for this problem. I know it is not the way but it will work till iOS 8 will be launched in the market.
Reason for Crash:
in iOS 7 [self.subViews objectAtIndex: i] returns UIView Instead of UIImageView and setImage is not the property of UIView and the app crashes. I solve my problem using this following code.
Check Whether the subview is UIView(for iOS7) or UIImageView(for iOS6 or earlier). And If it is UIView I am going to add UIImageView as subview on that view and voila its working and not crash..!!
-(void) updateDots
{
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView * dot = [self imageViewForSubview: [self.subviews objectAtIndex: i]];
if (i == self.currentPage) dot.image = activeImage;
else dot.image = inactiveImage;
}
}
- (UIImageView *) imageViewForSubview: (UIView *) view
{
UIImageView * dot = nil;
if ([view isKindOfClass: [UIView class]])
{
for (UIView* subview in view.subviews)
{
if ([subview isKindOfClass:[UIImageView class]])
{
dot = (UIImageView *)subview;
break;
}
}
if (dot == nil)
{
dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)];
[view addSubview:dot];
}
}
else
{
dot = (UIImageView *) view;
}
return dot;
}
Also, to clear the images that are already there, set the tint colors for the existing indicators to transparent:
- (void)awakeFromNib
{
self.pageIndicatorTintColor = [UIColor clearColor];
self.currentPageIndicatorTintColor = [UIColor clearColor];
}
Hope this will solve ur issue for iOS 7.
Happy coding
Try this link:-
Answer with GrayPageControl:-
Is there a way to change page indicator dots color
It is really good and reliable.I also have used this code.
You might have to do some more customization as
-(void) updateDots
{
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView* dot = [self.subviews objectAtIndex:i];
if (i == self.currentPage) {
if(i==0) {
dot.image = [UIImage imageNamed:#"activesearch.png"];
} else {
dot.image = activeImage;
}
} else {
if(i==0) {
dot.image = [UIImage imageNamed:#"inactivesearch.png"];
} else {
dot.image = inactiveImage;
}
}
}
}
Simply change the UIPageControl page indicator Color with pattern Image self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"circle"]];
The best compilation of the code for Swift 3 to replace the first icon of UIPageControl by a location marker:
import UIKit
class LocationPageControl: UIPageControl {
let locationArrow: UIImage = UIImage(named: "locationArrow")!
let pageCircle: UIImage = UIImage(named: "pageCircle")!
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 == 0 {
imageView = UIImageView(image: locationArrow)
} else {
imageView = UIImageView(image: pageCircle)
}
imageView!.center = view.center
view.addSubview(imageView!)
view.clipsToBounds = false
}
if i == self.currentPage {
imageView!.alpha = 1.0
} else {
imageView!.alpha = 0.5
}
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
}
}
Attached images:
I've created a custom page controller that should function in mostly the same way without hacking into the internals of a UIPageControl or having a whole library for one small widget.
Just place an empty UIStackView in your storyboard and make its custom class this class below, and use numberOfPages and currentPage just like a normal UIPageControl. Set the spacing on the UIStackView to change how much space there is between the views.
Swift 4.2
/**
If adding via storyboard, you should not need to set a width and height constraint for this view,
just set a placeholder for each so autolayout doesnt complain and this view will size itself once its populated with pages at runtime
*/
class PageControl: UIStackView {
#IBInspectable var currentPageImage: UIImage = UIImage(named: "whiteCircleFilled")!
#IBInspectable var pageImage: UIImage = UIImage(named: "whiteCircleOutlined")!
/**
Sets how many page indicators will show
*/
var numberOfPages = 3 {
didSet {
layoutIndicators()
}
}
/**
Sets which page indicator will be highlighted with the **currentPageImage**
*/
var currentPage = 0 {
didSet {
setCurrentPageIndicator()
}
}
override func awakeFromNib() {
super.awakeFromNib()
axis = .horizontal
distribution = .equalSpacing
alignment = .center
layoutIndicators()
}
private func layoutIndicators() {
for i in 0..<numberOfPages {
let imageView: UIImageView
if i < arrangedSubviews.count {
imageView = arrangedSubviews[i] as! UIImageView // reuse subview if possible
} else {
imageView = UIImageView()
addArrangedSubview(imageView)
}
if i == currentPage {
imageView.image = currentPageImage
} else {
imageView.image = pageImage
}
}
// remove excess subviews if any
let subviewCount = arrangedSubviews.count
if numberOfPages < subviewCount {
for _ in numberOfPages..<subviewCount {
arrangedSubviews.last?.removeFromSuperview()
}
}
}
private func setCurrentPageIndicator() {
for i in 0..<arrangedSubviews.count {
let imageView = arrangedSubviews[i] as! UIImageView
if i == currentPage {
imageView.image = currentPageImage
} else {
imageView.image = pageImage
}
}
}
}
Works for my purposes but I make no guarantees
Update for Swift 3.0 ... you know if you are OK with accepting stated risk: "Modifying the subviews of an existing control is fragile".
import UIKit
class CustomImagePageControl: UIPageControl {
let activeImage:UIImage = UIImage(named: "SelectedPage")!
let inactiveImage:UIImage = UIImage(named: "UnselectedPage")!
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 {
if let imageView = self.imageForSubview(view) {
if i == self.currentPage {
imageView.image = self.activeImage
} else {
imageView.image = self.inactiveImage
}
i = i + 1
} else {
var dotImage = self.inactiveImage
if i == self.currentPage {
dotImage = self.activeImage
}
view.clipsToBounds = false
view.addSubview(UIImageView(image:dotImage))
i = i + 1
}
}
}
fileprivate func imageForSubview(_ 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
}
}
You just need do it like this:
((UIImageView *)[[yourPageControl subviews] objectAtIndex:0]).image=[UIImage imageNamed:#"search.png"];
I think for this you need to customize whole UIPageControl. Please find more out at below links
How can i change the color of pagination dots of UIPageControl?
http://iphoneappcode.blogspot.in/2012/03/custom-uipagecontrol.html
From iOS 14 you can get and set the indicator image with these methods:
#available(iOS 14.0, *)
open func indicatorImage(forPage page: Int) -> UIImage?
#available(iOS 14.0, *)
open func setIndicatorImage(_ image: UIImage?, forPage page: Int)
From iProgrammer's answer
In case you want to hide the original dot
- (UIImageView *)imageViewForSubview:(UIView *)view {
UIImageView * dot = nil;
[view setBackgroundColor:[UIColor clearColor]]; << add this line
Following the previous answers I came up with the solution below.
Keep in mind that I had to add valueChanged listener that calls updateDots() as well in controller to handle taps made on UIPageControl
import UIKit
class PageControl: UIPageControl {
private struct Constants {
static let activeColor: UIColor = .white
static let inactiveColor: UIColor = .black
static let locationImage: UIImage = UIImage(named: "Location")!
}
// Update dots when currentPage changes
override var currentPage: Int {
didSet {
updateDots()
}
}
override func awakeFromNib() {
super.awakeFromNib()
self.pageIndicatorTintColor = .clear
self.currentPageIndicatorTintColor = .clear
}
func updateDots() {
for (index, view) in self.subviews.enumerated() {
// Layers will be redrawn, remove old.
view.layer.sublayers?.removeAll()
if index == 0 {
drawImage(view: view)
} else {
drawDot(index: index, view: view)
}
}
}
private func drawDot(index: Int, view: UIView) {
let dotLayer = CAShapeLayer()
dotLayer.path = UIBezierPath(ovalIn: view.bounds).cgPath
dotLayer.fillColor = getColor(index: index)
view.layer.addSublayer(dotLayer)
}
private func drawImage(view: UIView) {
let height = view.bounds.height * 2
let width = view.bounds.width * 2
let topMargin: CGFloat = -3.5
let maskLayer = CALayer()
maskLayer.frame = CGRect(x: 0, y: 0, width: width, height: height)
maskLayer.contents = Constants.locationImage.cgImage
maskLayer.contentsGravity = .resizeAspect
let imageLayer = CALayer()
imageLayer.frame = CGRect(x:0, y: topMargin, width: width, height: height)
imageLayer.mask = maskLayer
imageLayer.backgroundColor = getColor()
view.backgroundColor = .clear // Otherwise black background
view.layer.addSublayer(imageLayer)
}
private func getColor(index: Int? = 0) -> CGColor {
return currentPage == index ? Constants.activeColor.cgColor : Constants.inactiveColor.cgColor
}
}