Xcode 4, How to make other buttons fall off the screen after one is pushed. - xcode

As an example, I have 5 IBActions declared. When I push one, I want the other 4 to fall down off the screen. Any idea how to do this? Would I define a translation for each other IBAction??

A UIButton is a UIView. You can animate movement of a UIView; that movement can be to a position offscreen. (See the section under "animation" in the UIView class reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html) You might then want to remove the now-invisible buttons from their superview (in which case you'd need to retain them somehow or they might cease to exist), but you don't have to.
You will need a way to refer to the UIButtons. You can use IBOutlets for this, or you can "tag" the buttons in Interface Builder and use -[UIView viewWithTag:] to find the buttons you want to animate.
For a whole lot more on animation, see my book: http://www.apeth.com/iOSBook/ch17.html

Related

Add Custom Snap-Lines to UIView for Interface Builder

I have always wondered if there is a way to add custom "snap lines" to a UIView descendant, so I have a nice mechanism to layout my views using the Interface Builder of Xcode.
Here is my very basic UIView:
This view serves as a container control for other UIViews. Is there a way to add these lines so Xcode draws the "blue snap" lines when moving a control inside this view?
I know that there are UILayoutGuides but these don't add the desired snap lines to the Interface Builder (Xcode 9.3).
Here is another screenshot:
A workaround for this would be creating a view with a height of 1 and the width of its superview. After that, views with the same superview will snap to it.
Some tips:
Set a light background color for the view, so that it won't be distracting.
Remove the view when it's no longer needed. If you want to keep it for later usage, just hide it.

UIPage Control custom style NatGeo

In my app I have UIView that flow through the use of a horizontal scrollView. To scroll the view I used the classimo method [self addChildViewController: [self.storyboard instantiateViewControllerWithIdentifier: # "name Storyboard ID"]];
Each UIView is followed through the use of UIPageControl classic, but I do not like it and so I wanted to create something like this (see photo)
as you can see from the images above application there is a menu with a triangle pointing down, Indicating the page, passing from one topic to another user through the horizontal swipe on the scrollView
In other words, instead of having the classic shot for the management of the pages, the National Geographic has used a triangle pointing down and the title of the page on which the user is ...
Could someone help me understand how can 'be created a similar PageControl?
There are a lot of approaches to implement this. Maybe the custom PageControl won't be the subclass of a UIPageControl.
For example, you can subclass a UIView, and put a UIScrollView in it. And then put a bunch of UIButtons in a row into the UIScrollView, each of them has a title that is your page's title. When you tap a button ( you can get this event by UIButton's -addTarget:action:forControlEvents:), you can scroll the tapped button to the right position, highlight it, and put a little triangle below it.
There are many possibilities. Say, you can replace the buttons with UILabel and add UITapGestureRecognizer to capture the users tap, or you can replace the UIScrollView with UITableView if you get a long list of pages(Of course that will introduce some complexity). You just need to pick your favorite and try it out.

Moving a UIButton programmatically?

Right now I want to move the position of the button dependant if it is an iPhone 5 screen or not, but I am having trouble figuring out how to move the button, right now I have an "if statement" that can figure out if it is an iPhone 5 or not, but I don't know how to move my button inside the statement. Take a look at my code.
.h
-(IBAction)randomButton;
//the button I want to move
.m
- (void)viewDidLoad
{
[super viewDidLoad];
if (IS_IPHONE_5) {
//I don't know how to move the button in here.
}
}
The right place to put this programmatic layout is either in a layoutSubviews method (if, for example, the view controller's root view is a custom subclass) or in the viewDidLayoutSubviews method of your view controller class. In either of those methods, you can set the button's position and size by modifying its frame, like:
_theButton.frame = /*whatever*/
If you want to change the button's position without modifying the button's size you can adjust the button's center instead of its frame, but usually if you're doing programatic layout you want to set both the position and size in the same place.
If you need more complex layouts, you can also use 2 XIB files, one for iPhone 5 and one for iPhone 3/4. I typically do that as I can always see what it will look like without guessing or trial and error.

How to "turn off" transparency inheritance from UIView to controls inside the view in IB?

I have a UIView in Interface builder (Xcode 4.1)that has its alpha set to 0.1 .
On top of that view, there are some UIbutton objects. These buttons are children of that view, not simple positioned on top of it - this I clearly see in the object explorer.
The problem is, those buttons inherit the alpha settings from their parent view.
How can I turn this alpha inheritance off?
Old question, I know. I was just hoping for the same (although it does not make much sense) and tripped over this. No, it's not possible. The alpha value is always inherited from the parent value, overall transparency of the child view is a combination of own and parent's alpha.
I'm not sure but there should not be any problems caused by placing the buttons on top of the 'container' view instead of inside it. If you absolutely need a common handle for them you can wrap everything in e.g. another UIView.

How to "stick" a UIScrollView subview to top/bottom when scrolling?

You see this in iPhone apps like Gilt. The user scrolls a view, and a subview apparently "sticks" to one edges as the rest of the scrollView slides underneath. That is, there is a text box (or whatever) in the scrollView, that as the scrollView hits the top of the view, then "sticks" there as the rest of the view continues to slide.
So, there are several issues. First, one can determine via "scrollViewDidScroll:" (during normal scrolling) when the view of interest is passing (or re-appearing). There is a fair amount of granularity here - the differences between delegate calls can be a hundred of points or more. That said, when you see the view approach the top of the scrollView, you turn on a second copy of the view statically displayed under the scrollView top. I have not coded this, but it seems like it will lack a real "stick" look - the view will first disappear then reappear.
Second, if one does a setContentOffset:animated, one does not get the delegate messages (Gilt does not do this). So, how do you get the callbacks in this case? Do you use KVO on "scroll.layer.presentationLayer.bounds" ?
Well, I found one way to do this. When the user scrolls by flicking and dragging, the UIScrollView gives its delegate a "scrollViewDidScroll:" message. You can look then to see if the scroller has moved the content to where you need to take some action.
When "sticking" the view, remove it from the scrollView, and then add it to the scrollView's superview (with an origin of 0,0). When unsticking, do the converse.
If you use the UIScrollView setContentOffset:animated:, it gets trickier. What I did was to subclass UIScrollView, use a flag to specify it was setContentOffset moving the offset, then start a fast running timer to monitor contentOffset.
I put the method that handles the math and sticking/unsticking the child view into this subclass. It looks pretty good.
Gilt uses a table view to accomplish this. Specifically, in the table view's delegate, these two methods:
– tableView:viewForHeaderInSection:
and – tableView:heightForHeaderInSection:

Resources