Giter Club home page Giter Club logo

akpickerview-swift's Introduction

AKPickerView

Screenshot

Screenshot

A simple yet customizable horizontal picker view.

The Swift port of AKPickerView.

Works on iOS 7 and 8.

Installation

###CocoaPods: In your Podfile:

pod "AKPickerView-Swift"

And in your *.swift:

import AKPickerView_Swift

###Carthage: In your Cartfile:

github "Akkyie/AKPickerView-Swift"

And in your *.swift:

import AKPickerView

###Manual Install Add AKPickerView.swift into your Xcode project.

Usage

  1. Instantiate and set delegate and dataSource as you know,
self.pickerView = AKPickerView(frame: <#frame#>)
self.pickerView.delegate = self
self.pickerView.dataSource = self
  1. then specify the number of items using AKPickerViewDataSource,
func numberOfItemsInPickerView(pickerView: AKPickerView) -> Int {}
  1. and contents to be shown. You can use either texts or images:
func pickerView(pickerView: AKPickerView, titleForItem item: Int) -> NSString {}
// OR
func pickerView(pickerView: AKPickerView, imageForItem item: Int) -> UIImage {}
  • Using both texts and images are currently not supported. When you implement both, titleForItem will be called and the other won't.
  • You currently cannot specify image sizes; AKPickerView shows the original image in its original size. Resize your images in advance if you need.
  1. You can change its appearance with properties below:
var font: UIFont
var highlightedFont: UIFont
var textColor: UIColor
var highlightedTextColor: UIColor
var interitemSpacing: CGFloat
var viewDepth: CGFloat
var pickerViewStyle: AKPickerViewStyle
- All cells are laid out depending on the largest font, so large differnce between the sizes of *font* and *highlightedFont* is NOT recommended.  
- viewDepth property affects the perspective distortion. A value near the screen's height or width is recommended.
  1. After all settings, never forget to reload your picker.
self.pickerView.reloadData()
  1. Optional: You can use AKPickerViewDelegate methods to observe selection changes:
func pickerView(pickerView: AKPickerView, didSelectItem item: Int) {}

Additionally, you can also use UIScrollViewDelegate methods to observe scrolling.

For more detail, see the sample project.

Contact

@akkyie http://twitter.com/akkyie

License

MIT. See LICENSE.

akpickerview-swift's People

Contributors

akkyie avatar anas10 avatar ferannor avatar hyunseo0404 avatar josephkhawly avatar loicwolff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

akpickerview-swift's Issues

Problems when adding Navigation Controller

I sucessfully patched the Swift version to the new Swift 2 syntax. Everything works in principle, but only if I place the picker inside a normal view controller. Once I add a navigation controller, I'm getting error messages:

"Please check the values return by the delegate. The behavior of the UICollectionViewFlowLayout is not defined because: The item height must be less than the height of the UICollectionView minus the section insets top and bottom values."

This happens despite the picker has a size big enough to incorporate all items.

On screen, the problem is that the very last item is positioned below all other items, with an additional spacing to the top. This is why the space is not sufficient for everything. But this only happens when there is a navigation controller active.

What we put in "<#frame#>" ...

Hi ! :)
I'm a beginner in swift and my problem is that I don't know what we have to put in "<#frame#>" at the line self.pickerView = AKPickerView(frame: <#frame#>)...
thank you in advance for your answers !

Many errors in Swift 2

Hi Akkyie!

I think this component is not ready to use with swift 2.0
Someday will be possible?

Thanks!

Access control when using Cocoapods

When using cocoapods, the datasource, delegate and other properties are unavailable.

They are currently internal (i.e. can be accessed only within the AKPickerView module)

They should be set to public to allow access outside of the AKPickerView module

Swift 3 Support

@akkyie you have several PRs open for swift 3 support. Would you please consider merging one of them? Swift 2.3 is becoming less and less relevant and so will this excellent library if it's not updated at some point...

Wheel Perspective not Working

On 10.2, wheel perspective is completely absent on simulator and on device. Picker renders completely flat on .wheel mode. Playing around with viewDepth variable doesn't make any difference. Anyone else getting this issue?

Default positionning

Very nice pluggin Akkyie,

I have a request for you, is it possible to set the default positionning to the completly left and not directly at the middle ?

How to make Selected item Should be highlited

I am trying to change viewcontrollers using pickerview,But the selected item is not highlited while Changing viewcontrollers.This is my code ,Help me

func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) {
currentScrollIndex = item
print("currentScrollIndexSTEPS(currentScrollIndex)")
if item == 0{
let caloriesVC = self.storyboard?.instantiateViewController(withIdentifier: "CaloriesViewController")as! CaloriesViewController
caloriesVC.modalTransitionStyle = .crossDissolve
present(caloriesVC, animated: true, completion: nil)
}
else if item == 1{
let goalsVC = self.storyboard?.instantiateViewController(withIdentifier: "GoalsViewController")as! GoalsViewController
goalsVC.modalTransitionStyle = .crossDissolve
present(goalsVC, animated: true, completion: nil)
}
else if item == 2{
let oxigenVC = self.storyboard?.instantiateViewController(withIdentifier: "O2ViewController")as! O2ViewController
oxigenVC.modalTransitionStyle = .crossDissolve
present(oxigenVC, animated: true, completion: nil)
}

    preBtn.isHidden = (item == 0) ? true : false
    nextBtn.isHidden = (item == pickerViewItems.count - 1) ? true : false
}

Datasource in separate class

Hi,

I am trying to create few picker on same viewcontroller, now when i try to separate the datasource into another class in never shows the data, be it text or image.

here is the class and the way i am creating the picker.

class imagePickerDataSource : NSObject, AKPickerViewDataSource {
    func numberOfItemsInPickerView(pickerView: AKPickerView) -> Int {
        return imageInputs.count;
    }

    func pickerView(pickerView: AKPickerView, imageForItem item: Int) -> UIImage {
        let image = UIImage(named: imageInputs[item])!;
        return image;
    }
}

Here is how i am calling it.

 let imageDS = imagePickerDataSource();
 self.imageInputPicker.delegate = self;
 self.imageInputPicker.dataSource = imageDS;

The datasource functions are called but picker is never filled with image.

Carthage Build is not working in Xcode 7.2

If I'm using the Carthage build I always got the following error message:

Unknown class AKPickerView in Interface Builder file

I tried all solutions which were stated in StackOverflow for this issue but this one seems to be different.

I added the lib file to my project as usual, other frameworks are doing well.

Any idea to solve this issue?

Fonts for Title!!

Awesome work by everyone. ๐Ÿ‘

Any chance that titles would support fonts, in the near future?

func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) {
    print("Your favorite font is \(self.fontStyleArray[item])")
    selectedFontStyle = self.fontStyleArray[item]
    pickerView.font = UIFont(name: selectedFontStyle, size: labelFontSize)!
}

This doesn't change the font of the titles!!

Log warnings in Swift 3 + XCode 8

Just migrated my project that uses this library to Swift 3.0, it works but I have these warnings:

2016-09-21 12:43:37.621 MyApp[261:22982] the behavior of the UICollectionViewFlowLayout is not defined because:
2016-09-21 12:43:37.622 MyApp[261:22982] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2016-09-21 12:43:37.622 MyApp[261:22982] Please check the values return by the delegate.
2016-09-21 12:43:37.624 MyApp[261:22982] The relevant UICollectionViewFlowLayout instance is <_TtC18AKPickerView_SwiftP33_5835361EBAB06A1606BA941DE6A5477B22AKCollectionViewLayout: 0x16ed3e10>, and it is attached to <UICollectionView: 0x170e6000; frame = (0 0; 691 50); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x16d64f20>; layer = <CALayer: 0x16d65240>; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <_TtC18AKPickerView_SwiftP33_5835361EBAB06A1606BA941DE6A5477B22AKCollectionViewLayout: 0x16ed3e10>.
2016-09-21 12:43:37.624 MyApp[261:22982] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

Any idea how take out the alpha in the picker, just leave alpha 1 for all.

I would like to set the picker in the middle of his content, as example if i have

["Tokyo", "Kanagawa", "Osaka", "Aichi", "Saitama"]

I would like to show Osaka as the first.

like the method MyPicker.selectRow(row, inComponent: 0, animated: true).

And the way to don't use alpha at the left part of the picker.
I believe was in the attributes.alpha = fabs(currentAngle) < self.maxAngle ? 1.0 : 0.0; , but it didt work when i changed it .

Swift 2.0 on cocoapods

Hello,

Could you please tag a new version so the version on cocoapods uses swift 2.0.

Thanks

Crash on loading

We are getting this crash. I just thought I would mention it. Hope this helps.
screen shot 2016-08-24 at 2 27 03 pm

Support for VoiceOver

Thanks for the library! I'm implementing on an app that require VoiceOver support and I'm trying to...

  1. Let a VoiceOver user swipe thru the options without accidentally selecting one.
  2. Shift focus in the UI after the item has been selected to show related results (this one is more of a VoiceOver question for me at this point but I figured I'd share).

I've already added a new delegate for adding an accessibility label :)

Thanks!

Addendum: I copied this ticket over from the objc version of the library found here akkyie/AKPickerView#78

AKPickerView.selectedItem is not public

Did you mean to make the selectedItem field public? The comments suggest as much, and I'm not seeing another way to know which item is selected aside from an AKPickerViewDelegate, and that's overkill for me.

// MARK: Readonly Properties
/// Readonly. Index of currently selected item.

  • private(set) var selectedItem: Int = 0
  • private(set) public var selectedItem: Int = 0

attributedString

Would be nice if the delegate method returned an AttributedString instead of just String to allow for some special features like kerning.

delegate and datasource are not weak referenced and cause retain cycle

I am debugging a memory issue in my app and it appears, initially, that the datasource and delegate meathods in AKPickerView.swift are creating strong references back to my view controller that is causing it not to be de allocated. I changed lines 232 and 234 and 193 from

public var delegate:WHATEVER
to
weak var delegate:WHATEVER

and that cleared up the retain cycle.

I've been confused about when delegates in Swift need to be weak referenced and when they don't. I believe because your protocols are defined with @objc, they specifically need the 'weak' designation, where as if they were pure swift protocols that is not necessary. I could be completely wrong about that though.

Last item is not aligned properly

Hello there,

I have noticed an issue in your otherwise excellent picker: the last item of the collection is always misaligned (see attached image). This happens both in Wheel and Flat mode.

img_0016

Furthermore, I keep getting these warnings at runtime:

the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values. Please check the values return by the delegate.

Any suggestions?

Thanks.

pickerView custom class

In your example on the storyboard your picker view has a custom class assigned as AKPickerView instead of UIPickerView.
I cannot make this assignment when implementing the picker view in my project. How do I do this?

automaticallyAdjustsScrollViewInsets

Hey,

Just a small tip i have figured out:
If one of the parent view controllers of AKPickerView has self.automaticallyAdjustsScrollViewInsets = true, It might mess up AKPickerView's collectionView and you will get:

the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values.

error. Beware ! :)

Can not use in swift3 !!

When i build app, it is make 97 error in 'AKPickerView.swift'. I work swift 3
In podfile:
pod "AKPickerView-Swift"
and that success passing in terminal

Picker view not inside view constraints

grab

Hi,
I've been trying to resize the picker view so that the items are displayed inside the view.

In my storyboard I added a View, 100 x 40, and referenced it to AKPickerView. However when I run the code there is nothing inside the view. When I make the view bigger and run it the items are visible inside the bigger view.
The little rectangle right of 4 is an imageView which seems the lower the last value so that it is not in line with the rest. Is there a way to remove this?

How do I change the code to ensure that the items are displayed in the smaller View?

To make spacing work you have to alter both code and storyboard

Hi I want to change the spacing in-between images and have found in your sample that I have to do it in both the storyboard options and the code:
/// Readwrite. A float value which indicates the spacing between cells.
@IBInspectable public var interitemSpacing: CGFloat = 40.0

I don't know if its a mistake or its suppose to be like that. Thought I would tell you anyway.

Error enter the view

Hello, I'm using your library in my project and when I walk in this view where the picker view it gives the following error:

06/18/2015 14: 24: 11,480 [8670: 3,022,963] the behavior of the UICollectionViewFlowLayout is not defined because:
06/18/2015 14: 24: 11,480 [8670: 3,022,963] the item height must be less than the height of the UICollectionView minus the insets top section and bottom values.

Can you help me?
Thanks

Horizantal pickerview at Center

I have Three images ,i want by make default second image at middle ,but i can't do it .Help me

func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) {
currentScrollIndex = item
preBtn.isHidden = (item == 0) ? true : false
nextBtn.isHidden = (item == pickerViewItems.count - 1) ? true : false
if item == 0{
let caloriesVC = self.storyboard?.instantiateViewController(withIdentifier: "CaloriesViewController")as! CaloriesViewController
caloriesVC.modalTransitionStyle = .crossDissolve
present(caloriesVC, animated: true, completion: nil)
}
else if item == 1{
let goalsVC = self.storyboard?.instantiateViewController(withIdentifier: "GoalsViewController")as! GoalsViewController
goalsVC.modalTransitionStyle = .crossDissolve
present(goalsVC, animated: true, completion: nil)
}
else if item == 2{
let oxigenVC = self.storyboard?.instantiateViewController(withIdentifier: "O2ViewController")as! O2ViewController
oxigenVC.modalTransitionStyle = .crossDissolve
present(oxigenVC, animated: true, completion: nil)
}
}
func pickerView(_ pickerView: AKPickerView, imageForItem item: Int) -> UIImage {
return UIImage(named: "flag_(item)")!.imageWithSize(CGSize(width: 49, height: 49))
}

'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path:

When calling the selectItem just after calling reloadData, we get a crash "'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path:".

pickerView.reloadData()
pickerView.selectItem(item)
Need to know what is the best way to call selectItem after the making sure the collection view inside the AKPickerView is loaded.

Framework Error when adding pod.

I got this error when adding pod dependency:
[!] Pods written in Swift can only be integrated as frameworks; this feature is still in beta. Add use_frameworks! to your Podfile or target to opt into using it.

Can't scroll in table view header

I have an AKPickerView in a UITableView header section, and it won't scroll horizontally. I've tested the implementation outside of a table view subclass and it works fine, so it shouldn't be a problem with the library. Just wondering if this problem should be expected and how to fix it. Thanks!

Two lines

Hi,

Thanks for your repo.

I need to make it two lines, is there a way ?

Thanks.

Best regards,
Mariam AlJamea.

Swift 3.1 Warning for 'M_PI_2' is deprecated:

Upgraded to Swift 3.1 I get warnings: 'M_PI_2' is deprecated: Please use 'Double.pi / 2' or '.pi / 2' to get the value of correct type and avoid casting.

I have solved it with Double.pi / 2 as it says.

Just to solve it to remove the warning.

Cannot add Custom Class

Hi,

I cannot add the custom class AKPickerView to the UIPickerView in the Storyboard. I think that due to this I keep getting fatal error: unexpectedly found nil while unwrapping an Optional value.
Can you please explain how to do this?

Thanks.

Selected Item Highlight Text

The highlightedFont is set to bold. However, when you select another item the previous item remains bold and the new item's font does not become bold. When the first item is scrolled off the screen it is redrawn and the font is set to the system font rather than the bold font.

It would appear that the issue is with the selected implementation of the AKCollectionViewCell. Changing this from _selected to selected removing the initialisation to false and updating the setting in the cellForRowAtIndexPath fixed the issue for me.

Has anyone else had this issue? Could it be the way I am initialising the picker or is this a genuine issue others have experienced?

Compilation errors

If I try to run this in Xcode 7 beta 4, I get 9 compilation errors. Maybe the Swift language framework has changed in the meantime?

Detect the value when I scroll .

Im trying to get the values of the picker when it is scrolling, i believe it was the method scrollViewDidScroll but it never get called.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.