I'm trying to build a custom, reusable UIScrollView that can be added to multiple views. The scroll view is going to be a weight picker. For the life of me, I can't find a decent example for how to implement this neatly or cleanly.
I would love for someone to point me to an existing library or tutorial that shows me how to do this. I've hacked apart a few examples, but so far, nothing is very good or reusable. Please help!!
For what it's worth, I have an image that individual ticks for the weight. So I can select to the tenth of the number (e.g. 160.4). The image has the first tick bold and larger than the remaining 9. I'd like to have the weight/number centered over the large tick. I'll update the points to my label/datasource after scrolling stops.
UPDATE
I need to make this. I have the custom font, background, and ticker image.
I would not do this through an UIScrollView. I think it would be more complex and you would certainly end up having issues when trying to add you custom picker into another scroll view.
What I would do is:
building the picker view by means of a series of CALayers, each one representing a "building block" of your picker view; see attached image:
each building block would represent a specific value by mixing a UILabel (the text) and an image;
use a pan gesture recognizer, or alternatively define touchesBegin/Moved/Ended method to deal with panning;
when a pan is done, you displace the view content to the left or right according to the panning;
when panning, you also add new building blocks to the left or right end of the picker to account for empty areas that would be revealed by the displacement done at point 4.
I think that having a look at another kind of custom control source code would be of great help to you. You would not possibly find your custom picker already implemented, but could get some guidance. Have a look then at cocoa controls.
Hope this helps.
If I were going to implement this, I would create a really wide image that had every weight on it I'd ever need - I would probably create this in code when the app started up. This image is then used as the contentView of your picker. You get all the scrolling features "for free", and you could even update the values shown in the other parts of the view during scrolling (or dragging.
The scrollView is just the area with the tick marks and weight numbers, and resides in a subview above the background, but below the centered vertical line that shows the actual weight.
EDIT: on second thought, forget the image. If you have the code to draw the image, you can do the drawing in a custom UIView. So you get the draw rect, you know the contentOffset, so you can draw just what you need.
Related
As you can probably tell from my last few questions, I am working on a screen that allows users to edit labels. Pages and all the other lovely apps have glorious resizing handles to show what label is currently being edited.
I am trying to get some feedback on the best way to do this. Is it possible to add an 'dot' image to the centre of all the edges of the label? Is it possible to change the background colour only in sections of a label?
Or should I add an image on top of the label and disable it? The most important feature is for only the uilabel to respond to touches. If I overlay an image, I want it to ignore the touch and hand it off to the label.
Any feedback appreciated!
Cheers Guys!
You will find as many opinions on this as there are programmers. There is not built-in decoration for a view that shows the dot in the middle of each side. But you can add that easily enough. One way is instantiate four instances of an image with the dot and add them as subviews of the view you are decorating. you can then set the center of each dot on the center of each edge in the parent view. As long as clipping is turned off in the parent view, these will show up (if they are not hidden) even if they extend outside the frame of the parent view.
I don't understand what you want to accomplish by changing the background color in sections of the label view, but I this probably has some moderate complexity to it.
You can also to this with core graphics. I'll stop there, though. There are literally many ways to do what you are trying to do.
What I'm trying to do:
The bottom (and top) cells of a "grouped style" UITableView have rounded corners - as is the default in many Apple iOS and non Apple apps. Sometimes cells are inserted via an animation, i.e:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
Now this works fine. Except: When a cell is inserted below the bottom cell, or above the top cell (for a grouped style table) - there seems to be no smooth animation.
The Problem:
What I mean is that the corners of the top/bottom cells need to animate from rounded to unrounded (as they are no longer top/bottom) - and this happens in a very jerky and unlike Apple fashion.
Does Apple ever do this in an App - if so, how do they do this smoothly? Otherwise, how could this be done right?
NB - I know this is a pretty minor detail, but I'm a bit of a perfectionist...!
Take a look at the Weather App that comes with iOS, in the flip side view where you configure your cities you will see a table that accommodates these animations. As Mark Adams points out, in iOS5 there is an enum for the OS to auto-detect which animation should be used. If you want to target pre-iOS5 then on your delegate method that commits deletion you should determine what cell is being deleted based on its row position, if its 0 you would use UITableViewRowAnimationBottom if it is the the last row you would use UITableViewRowAnimationTop and if it is somewhere in the middle you would likely use UITableViewRowAnimationTop so that the group shifts upward with the animation, you might want to play around with this though.
Sorry I don't have time to write actual code out and test it, but maybe I can give you a lead. You're basically asking to set the corner radius on only two corners of a cell, then fluidly animate them to sharp corners as they move toward the center of the table.
One way to do that might be to define your custom UITableViewCell with a CAShapeLayer as the background. CAShapeLayer is drawn from Quartz paths: see http://developer.apple.com/library/IOS/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_paths/dq_paths.html#//apple_ref/doc/uid/TP30001066-CH211 . CAShapeLayer's path is animatable, but not with implicit animation. You'll need to use CAPropertyAnimation or perform your own interpolation. In my experience, for example in http://itunes.apple.com/us/app/blaster-through-asteroid-field/id414827482?mt=8 , doing your own interpolation is faster and more reliable (but other than the interpolation and collision detection everything most of the game relies on quartzcore, so I don't think you'll have a performance issue).
SUMMARY: Custom cell has CAShapeLayer backing. CAShapeLayer has path with 2 rounded corners. You animate the path property as the position of cell moves toward a certain point on screen. Done, smoothly animated rounded-to-square corners. Let me know if you try it out :-)
Other references:
http://developer.apple.com/library/IOS/#documentation/GraphicsImaging/Reference/CAShapeLayer_class/Reference/Reference.html#//apple_ref/doc/uid/TP40008314
http://developer.apple.com/library/IOS/#documentation/GraphicsImaging/Reference/CGPath/Reference/reference.html
As there's no real (code complete) answer currently, here's a list of the best workarounds.
Change your interface so that this jerky animation never happens.
This might mean using a plain style tableView as opposed to grouped
style, or using the alternate style seen in the Weather app (see
Chris Wagner's answer. Thanks Chris!). It might even mean using a
completely different interface that doesn't incorporate this
animation - this was the solution I went for, I managed to work out
a UX alteration that is possibly even better than the original.
Deal with it. Apple seem to, in some of the less used parts of
the OS or their apps.
File a bug report on bugreport.apple.com. It would be nice if Apple
fixed this... I know it's a pretty minor detail, but that's why I
like Apple products - cos they get the little details right!
Create a custom UITableViewCell subclass. I've thought a bit about
this, and while it's a bit of a programming challenge, I think it's
doable. Here's how: EDIT: See Rab's answer for some more detail. Still haven't coded it out yet but may give it a go when I get some time.
The custom cell has a property that defines whether the cell is a top, middle, or bottom cell. This can be set by a UITableViewController (or subclass etc).
The backgroundView property of the custom cells can be custom drawn using quartz for the rounded corners etc.
Whenever the property that defines the position of the cell is altered, the change to backgroundView can be animated. The hard part
here is animating the curve of the corners... is this doable? Apple
seems to do it in the corners of notification center, when it is
pulled down and pushed up - so seems like it is possible.
If someone seriously want to do this, and put the code on gitHub... you would be awesome! And you would have bettered
Apple's UI ;)
Have you tried to insert two rows at a time? The one on the top with zero height, and the next with rounded corners (you can use quartz core for that)
If you check the Tags section of the 'Send to Youtube' part of the Photos app, you'll see apples animations are just as 'jerky' as the rest of ours. (obviously you need a video to upload to youtube)
I'll assume thats what you were thinking of...
I've not much response so am adding some more info.
My buttons are not rectangular, nor organised in a grid so I need a way of creating what looks like a button (and shows that it has been pressed visually, as per a standard UIButton) but where the touchable area is different to the image area.
I am using a transparent PNG and that element works fine. I've added the buttons in Interface Builder and am wondering if that is the problem.
However, if I change imageEdgeInsets, it distorts the image display, which is obviously not what I want.
Bizarrely, if I increase the dimemsions of the button, it doesn't change the image, but if I decrease them it does.
I have tried different combinations of mode (scale to fill etc), but to no avail.
I am aware that there is an image and background image property, but in IB there is only one.
Essentially, I don't understand how the geometry works and the Apple documentation doesn't seem to help.
Surely, I can't be the only person to try to do this. Any help would be warmly welcomed.
Many thanks,
Chris.
Try setting the buttons setting to Aspect Fit. This will fill the button with your image so a smaller image than the button size would leave the space around the image.
Also set the button type to custom.
In the end, I stumbled across Ole Begemann's Non-rectangular buttons class. It just does what I need - to be able to create buttons where the touchable area follows the visible element of a non rectangular image.
#Helium3 - thanks - that allowed me to use a larger touch area, bit not a smaller one.
I've had this issue come up a couple times doing iPhone development now and have yet to find a good solution for it. What I'm looking for is this:
I want to show some text along with an icon and I want the overall display to be centered within its parent view. Specifically, in my current case, I'm looking to display a box that says "Reconnecting..." with a UIActivityIndicatorView to the left of the text.
Previously, I've just relied on the fact that I know exactly the dimensions of the text and activity indicator, so I can position things absolutely to appear centered. What I'm looking for is something more automatic.
Any ideas?
One of the UIKit additions to NSString will return the pixel size of the text if you give it the font that you're using on the UILabel. So then presumably the total size of the two things together in the layout you describe is:
the difference between label.frame.origin.x and activityIndicatorView.frame.origin.x; plus
the width of the text.
You can then either shuffle both the views appropriately or give them a common parent that's still a subview of the whole thing and shift that.
An activity indicator can be shown in a view just calling a single method.Please click here get SHKActivityIndicator class
I'm trying to figure out if I can get what I want out of UIScrollView through some trickery or whether I need to roll my own scroll view
I have a series of items in row that I want to scroll through. One item should always be centered in the view, but other items should be visible to either side. In other words, I want normal scrolling and edge bouncing, but I want the deceleration when the user ends a touch to naturally settle at some specified stop point. (Actually now that I think of it, this behavior is similar to coverflow in this respect.)
Take a look at this project: SwipeView
Good luck
I'd check out https://github.com/nicklockwood/iCarousel, it's awesome.
You should take a look at the "pageingEnabled" property of a UIScrollView. Basically you can define a size of a segment and the number of segments. Then it will automatically stop to adjust to that specific segment like you want. If you set the segments to be smaller than the actual width of the screen I think you should get the behaviour you are looking for.