Code Monkey home page Code Monkey logo

akpickerview's Introduction

AKPickerView

Build Status Gitter

Screenshot

Screenshot

A simple yet customizable horizontal picker view.

Works on iOS 6, 7 and 8.

News: Say Good-Bye to Bridging-Header! The Swift version is here.

Installation

Use CocoaPods

pod "AKPickerView"

…or simply add AKPickerView.h/m into your project.

Usage

Caveat: From version 2.0, dataSource separated from delegate. When you updated it, check new how to use below.

  1. Instantiate and set delegate and dataSource as you know,

    self.pickerView = [[AKPickerView alloc] initWithFrame:<#frame#>];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
  2. then specify the number of items using AKPickerViewDataSource methods,

    - (NSUInteger)numberOfItemsInPickerView:(AKPickerView *)pickerView;
  3. and specify contents to be shown. You can use either texts or images:

    - (NSString *)pickerView:(AKPickerView *)pickerView titleForItem:(NSInteger)item;
    // OR
    - (UIImage *)pickerView:(AKPickerView *)pickerView imageForItem:(NSInteger)item;
    • Using both texts and images are currently not supported. When you implement both, -pickerView: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.
  4. You can change its appearance with properties below.

    @property (nonatomic, strong) UIFont *font;
    @property (nonatomic, strong) UIFont *highlightedFont;
    @property (nonatomic, strong) UIColor *textColor;
    @property (nonatomic, strong) UIColor *highlightedTextColor;
    @property (nonatomic, assign) CGFloat interitemSpacing;
    @property (nonatomic, assign) CGFloat fisheyeFactor;
    • All cells are laid out depending on the largest font, so large differnce between the sizes of font and highlightedFont is NOT recommended.
    • fisheyeFactor property affects perspective distortion. The range is 0.0 - 1.0; slight value such as 0.0001 is recommended.
  5. After all settings, never forget to reload your picker.

    [self.pickerView reloadData];
  6. Optional: You can use AKPickerViewDelegate method to observe selection changes:

    - (void)pickerView:(AKPickerView *)pickerView didSelectItem:(NSInteger)item;

    Additionally, you can use UIScrollViewDelegate method to observe scrolling.

For more detail, see the sample project.

Contact

@akkyie http://twitter.com/akkyie

License

MIT. See LICENSE.

akpickerview's People

Contributors

akkyie avatar gevazeichner avatar gitter-badger avatar phatmann avatar rinatkhanov avatar tifroz avatar wcwynn 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

akpickerview's Issues

How can I get default selected item at first run???

My goal is very simple: retrieve title of default item at first run so I don't need to move scrollview to listen to delegate - (void)pickerView:(AKPickerView *)pickerView didSelectItem:(NSInteger)item.

HighlightedFont not updating upon selection change

When I set the highlightedFont property with a larger font than the normal font property, the first item loaded has a larger font, but it remains that way no matter what item is selected thereafter. Please fix. Ideally, the font size would gradually increase in transitioning to the larger size, rather than abruptly changing to the larger size upon selection (and vice versa for deselection). Thanks for a great job on this!

Infinite Scroll?

Is it possible to have, say, 5 values on infinite scroll, so that there would be no left or right end? Much how date pickers on iOS, when the month passes December it just starts right up again in January?

Swift port

Awesome work! However, would there be any intention on having a swift version of the library?

AKPickerView dealloc is not getting called

At first, I thank you for providing such a control, it really saved us a lot of time.

I have a memory Issue related to AKPickerView, though not very sure, if its related.
I have a custom UITableViewCell designed in StoryBoard, which contains "AKPickerView".
When the controller gets dealloc, the tableview gets dealloc'ed, and eventually all of its subview must get dealloc'ed, but the cells hang around and aren't dealloc'ed.
Inspecting with Instruments, i see a lot of AKCollectionViewCell's hanging around.

As said above, am not very sure, if the issue is just because of AKPickerView.
It would be great, if i get some help on it.

Thanks!

Support for UIView

Would like to see a

  • (UIView *)pickerView:(AKPickerView *)pickerView viewForItem:(NSInteger)item;

delegate method.

UITableview usage

Hello and thanks for the great code.
Is there any way to use this in a tableview in a settings style view?
each row that consists of this control should have each own datasource,values.

again thanks for contributing

Crashes when datasource array is empty.

It crashes on line #188 when number of items in datasource array is 0.

NSAssert(item < [self.collectionView numberOfItemsInSection:0],
         @"item out of range; '%lu' passed, but the maximum is '%lu'", item, [self.collectionView numberOfItemsInSection:0]);

How do I set function scrollViewDidEndDecelerating using AKPickerView

I use UIScrollViewDelegate protocol and try this:

func scrollViewDidScroll(scrollView: UIScrollView) {
    self.pickerView.backgroundColor = UIColor.whiteColor()
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    self.pickerView.backgroundColor = UIColor.clearColor()
}

but it's not work because scrollViewDidEndDecelerationg method will call when scroll end but AKPickerView will scroll automatic to title and call scrollViewDidScroll again.
How do I solve this ?

Thank you

Enhancement: Allow disabling of 3D effect for use with flat UI's

@akkyie I'm currently using your picker in an app with a flat design and I was looking around to see if there was an easy place to add a property on AKPickerView to disable the 3D effect. I was thinking of adding a AKPickerViewStyle property with AKPickerViewStyle3D and AKPickerViewStyleFlat possible values. Or if you have a better solution? Thanks

EXC_BAD_ACCESS crash

Hello! I have installed your code and really like the design and functionality. Nice work! However, I have discovered that the delegate code crashes with an EXC_BAD_ACCESS fault if the AKPickerView object is released inside the -pickerView:didSelectItem: delegate method. This can be resolved by adding the following code to the AKPickerView implementation:

- (void)dealloc
{
    self.collectionView.delegate = nil;
}

Warning for modifying UICollectionViewLayoutAttributes

This throws a warning for modifying these attributes without copying. If you throw a copy in this line, it goes away.

 UICollectionViewLayoutAttributes *attributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];

Enhancement: Allow picker view to contain NSString or UIImage items similar to UISegmentControl

One useful addition might be for enabling support of items that are NSStrings or UIImages similar to what is allowed with UISegmentControl. UISegmentControl supports setting an image or a title (not both) for each segment using the following interface:

- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment;
- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment;

I haven't given much thought on how to implement this yet other than possibly adding a new AKPickerViewDelegate method:

- (UIImage *)pickerView:(AKPickerView *)pickerView imageForItem:(NSInteger)item;

And adding an image property to AKCollectionViewCell:

@property (nonatomic, strong) UILabel *image;

And a mode flag (specified at creation?) indicating which delegate method should be called and which collection view cell property will be populated.

I'm not crazy about the idea of a mode flag so maybe this isn't the best approach to adding this functionality.

An alternative idea would be to allow for optional setting both a title and image depending on if either or both delegate methods are implemented. In this way, the mode flag would not be necessary and the cell could do something like place the label above the image. This approach is probably a better solution.

How to set default selection on start

As above. Since it defaults to 0, how can i make it change to other index? It can either animate to my index on load, or immediately show it there.

UICollectionViewFlowLayout cache mismatched frame (iOS9, xCode7)

AKPickerView outputs this when running on iOS9, xCode7:

Logging only once for UICollectionViewFlowLayout cache mismatched frame
UICollectionViewFlowLayout has cached frame mismatch for index path <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0} - cached value: {{170.66667847961008, 0}, {72.999976338098122, 736}}; expected value: {{170.66666666666666, 0}, {73, 736}}
This is likely occurring because the flow layout subclass AKCollectionViewLayout is modifying attributes returned by UICollectionViewFlowLayout without copying them

Problems with example

Hello! First of all I want to thank you for such a great work!

Now I have a small problem I cant figure out. I copied the example ViewController to my project, created a new view and etc. Everything works fine except two things:

  1. I always get the errors in NSLog (from didSelectItem: method):
    2014-12-23 20:58:29.569 XXX[21526:807229] the behavior of the UICollectionViewFlowLayout is not defined because:
    2014-12-23 20:58:29.569 XXXX[21526:807229] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values.
    2014-12-23 20:58:29.570 XXXXX[21526:807229] Please check the values return by the delegate.

  2. The last item in the created view is always below the others...(see screenshot below)
    screen shot 2014-12-24 at 10 52 16

What can be possibly be wrong? I cant figure it put for already two days))

Segment Width

It seems like there is no way to make the width for each segment/item equal?

Doesn't Work With Autolayout

I'm using this inside a UIView with auto layout. The picker itself works with auto layout fine, but the collection view it's using holds the initialized empty frame. Can you add constraints to the collection view to work with auto layout?

I need loop

Thanks for AKPicerView first

And I need AKPickerView support loop now,but I could't modify,please help me.

Thanks very much again.

How to initialize a value for AKPickerView

I have numbers from 50 to 200.
I want to auto scroll to 100 when PickerView can be seen.

I used to [self.pickerView scrollToItem:100 animated:NO] after [self.pickerView reloadData]
But it can't working.

Is there a way to solve this problem?
Thank you!

High CPU Usage and sluggish when there are 100s of objects

When we add more more objects as items say like this

self.titles = [NSMutableArray new];
for(int i =0 ; i<1000;i++){

    [self.titles addObject:[NSString stringWithFormat:@"Object %d",i]];
}
[self.pickerView reloadData];

The picker view is very slow to respond and very high cpu while scrolling ....
Any clue?

Height of interactive region does not match height of the picker view

With the current implementation, the interactive region of the picker view is limited to the height of labels contained in the picker view (and these labels are sized to fit the specified titles). This can make it difficult to interact with the picker view in some cases, in particular for smaller fonts.

To observe this issue, the sample picker view app can be modified as follows:

diff --git a/AKPickerViewSample/AKViewController.m
--- a/AKPickerViewSample/AKViewController.m
+++ b/AKPickerViewSample/AKViewController.m
@@ -21,7 +21,8 @@

 {
     [super viewDidLoad];

-       self.pickerView = [[AKPickerView alloc] initWithFrame:self.view.bounds];
+       self.pickerView = [[AKPickerView alloc] initWithFrame:CGRectInset(self.view.bounds, 0, self.view.bounds.size.height/ 2.5)];
+       self.pickerView.backgroundColor = [UIColor redColor];

To address this issue, one solution would be to modify the interactive region to be the full height of the picker view. After making the change, the picker view background will be red but interaction will be limited only to the region of the screen containing the labels.

Last version is not working

Hi,
I'm trying to use your interesting picker.
I imported it in my project using cocoapods but it's not working. The picker simply isn't showing up...
But if I use classes included in the sample project, than it magically works without any other modification of my code.
BTW, my code is exactly equal to yours, I did a "Hello world" app trying to understand why it isn't working.
It seems that initialization (font, text color, ...) now is mandatory.

There is also an issue in collectionView:layout:insetForSectionAtIndex:
if the collectionView has no elements, the app crashes in this line of code (due to lack of check on number!=0)

CGSize firstSize = [self collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:firstIndexPath];

bug in highlightedTextColor

When repeated pressure on the edge of the view turns writing color within the view to the highlightedTextColor

{

self.viewHeightPicker.delegate = self;
self.viewHeightPicker.dataSource = self;
self.viewHeightPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.viewHeightPicker.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];
self.viewHeightPicker.highlightedFont = [UIFont fontWithName:@"HelveticaNeue" size:14];
self.viewHeightPicker.highlightedTextColor = [UIColor colorWithRed:1 green:0.41 blue:0.18 alpha:1];
self.viewHeightPicker.textColor = [UIColor darkGrayColor];
self.viewHeightPicker.interitemSpacing = 20.0;
self.viewHeightPicker.fisheyeFactor = 0.0005;
self.viewHeightPicker.pickerViewStyle = AKPickerViewStyle3D;

}

screen shot 2015-03-16 at 2 28 30 pm

display issue

hey I also have a display issue for my pickerView (all the code I put in the other issue is the same)

as you can see the first item is correctly positionned, but the other one is lower and I don't understand why

picker

How can I customise the title in order to have a two line label title?

Hi,
I've been checking your framework out to use it in a project of mine.
I would like to build something similar to this print:
image
I was wondering if there is a way to customise the label or the picker view itself in order to have a two line title. It would be a title and a subtitle.
Thank you for the attention.

First item stays selected

Bug where at init the first item stays selected. (workaround, programmatically setting the the first item after init)

Saving selection when reloading view.

Awesome project for starters.

I have this embedded in a table view cell. Everything is implemented, however, I'm stuck on how the selection gets saved.

More insight on my project:
I have a left table view with multiple selections. One of which is a settings view. The settings view is a table view. In one of the cells is this project. I'm using this to give the user a selection of animations they want to use throughout the app. Zoom Out, Drop In, or the default Push navigation controller.

In the cellForRowAtIndexPath:

                //Animation Picker
                self.pickerView = [[AKPickerView alloc] initWithFrame:cell.bounds];
                self.pickerView.delegate = self;
                self.pickerView.dataSource = self;
                self.pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                [cell addSubview:self.pickerView];

                self.pickerView.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
                self.pickerView.highlightedFont = [UIFont fontWithName:@"HelveticaNeue" size:20];
                self.pickerView.interitemSpacing = 25.0;
                self.pickerView.fisheyeFactor = 0.0002;
                self.pickerView.pickerViewStyle = AKPickerViewStyle3D;

                zoom = @"Zoom Out";
                drop = @"Drop In";
                push = @"Push";

                self.titles = @[zoom, drop, push];

                [self.pickerView reloadData];

Then those get called in your didSelectItem below:

//Selection
pickerView:(AKPickerView *)pickerView didSelectItem:(NSInteger)item {
if ([self.titles isEqual:zoom]) {
}
else if ([self.titles isEqual:drop]) {
}
else if ([self.titles isEqual:push]) {
}
NSLog(@"%@", self.titles[item]);
}

Each one of those I plan on putting in some NSUserDefaults, so they correspond to the correct animation chosen.

The issue at hand is when leaving then returning to the settings view, it gets reset to the first item.

I've never used something like this before and I feel it's something simple I'm missing, any insight or help would be appreciated.

Swipe at end of list causes animation redraw jankiness (Swift/Obj-C, iOS8.2, iPhone 5/6)

Hey there!

This is a great looking plugin! Can't use though due to a bug I've found. Wondering if this is known?

Basically take the demo app and run on iPhone 5 or 6 with iOS 8.2. Swipe to left or right-most item. Stop. Attempt to swipe past that last item on the list and the list will redraw and reanimate from its original position. Attempt to swipe back towards the rest of the list and the same thing will happen.

Moving the list slowly seems to be okay but it has to be very slow.

Only happens on end items.

Changing number of items doesn't help.

Only tried with text (not icons).

Am I missing something here? Would love to use this!

Let me know how I can help if possible!

Best,
C

reloadData pulls in new data, but spacing for old data persists

I've got an AKPickerView which changes depending on user settings. It is in a recipe conversion tool and displays cups, grams (g) or ounces (oz) depending user settings. If user selects different units, then I change the data source array for the picker.

Changing the array works fine, and the correct list of items appears according to the unit (cup/g/oz) selected by user.

But it seems that the spacing between items is dictated by the array first loaded. If I change the array, then the new items are displayed, but the spacing is wrong and the selected item gets forced off centre (at least, I think that's why it is not centered).

Looks fine on first display:

screen shot 2015-02-13 at 17 41 44

But after changing data in that picker, it goes a bit wonky:

screen shot 2015-02-13 at 17 41 22

I can work around it by using items with very similar character lengths in each data set, but is there a way to force the picker to take into account the length of the items in the new data so that items are spaced out correctly? Any help much appreciated, thank you so much!

Scroll one cell at a time

Hello,
Thanks for your great library!

I would like for it to sort of scroll/snap one cell at a time when scrolling side to side.
When I scroll, the position is after 2 or 3 or more items. I want property like pageenabled so would like it to work like apple camera. (Apple camera move only one cell at a time)

When I tried with pageenable property and targetContentOffsetForProposedContentOffset:withScrollingVelocity of UICollectionViewFlowLayout, I have lots of problems. Can you make more enhancement for me?

Thanks for your great library again!
Thanks,

Loannis

Not showing when embedded in nav controller for a specific height on iOS 8

Hi!
I really like your picker but I can't get the picker to show if I modify the example code by setting the size of the picker to:
self.pickerView = [[AKPickerView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 60)];
and embed in a navigation controller running iOS 8 in the simulator, works fine in iOS 7 though or without the nav controller.

Displaying two different AKPickers at the same time

I’m new to iOS coding, and I'm struggling to get two different AKPickerViews to display at the same time. Any help would be really appreciated!

I’ve tried this:

- (void)viewDidLoad {

[super viewDidLoad];
self.picker1 = [[AKPickerView alloc] initWithFrame:CGRectMake(150, 100, 150, 50)];
self.picker1.delegate = self;
self.picker1.dataSource = self;

// rest of the picker1 settings here

self.titles1 = @[@"4",
                         @"5",
                         @"6",
                         @"7",
                         @"8"];

[self.picker1 reloadData];

self.picker2 = [[AKPickerView alloc] initWithFrame:CGRectMake(150, 300, 150, 50)];
self.picker2.delegate = self;
self.picker2.dataSource = self;

 // rest of the picker2 settings here 

self.titles2 = @[@"a",
                         @"b",
                         @"c",
                         @"d",
                         @"e"];

 [self.picker2 reloadData];

 }

But when I come to implement the numberOfItemsInPickerView and titleForItem methods I have to use one array or the other (titles1 or titles2) to get it to work. So I can get two pickers to display but both have the same data.

I guess I don't know how to implement those methods where there are two pickers:

- (NSString *)pickerView:(AKPickerView *)pickerView titleForItem:(NSInteger)item
{
return self.<<what do I put here so that picker uses data from the relevant array?>>[item];
 }

Can someone guide me towards how I can display two pickers at the same time, each independent of the other? How do I get the picker to use data from the 'correct' array?

DidSelect

Hello,

I'm using AKPicker view in my app, and the didselect method gets called 4 times when my view appears, I have created a method to init the picker view where I set all its properties, then I added reloadata and added it to my view.

My problem is that I want to scroll a page view controller (views inside are collection view) depending on which item is selected in the pickerView, but it gets called too many times on appearance, thus creating a loop in my app.

can you help?

thanks

AKPickerView not loading from delegate

Ok, so I just realized that the 2.0 that I upgraded to last week was just returning an empty AKPickerView (I had a pretty simple implementation via storyboards). I've downgraded to 1.3 and its working fine again. Just curious if this issue has been fixed (I'm not sure exactly what the error even is) in 2.1 or 2.2?

Add support for both images and text at the same time

Like the title says. It would be useful to copy Android's scrolling tabs functionality. Issue #47 would also be able to fix this but might take more time to implement.

Can you please take a look at this if you can find the time?

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.