I have the following which works fine for somethlike a UIImageView. I am not sure if a UIView animation would work with a contained controller? I know I am animating the view's frame but just not sure if this would / should work (the below code is just proof-of-concept). But even if it did, the below is not working. Any ideas on why not?
thx in advance
edit #1
I've added a screen shot that shows a custom view controller in yellow with the phrase holla in it. As you can see the below code aniamtes the frame but not the elements of that view controller. Is this intended? Is there a workaround?
#import "MyViewController.h"
#interface ViewController ()
#property (nonatomic, strong) MyViewController *firstIV;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
// lets add a few UIImageViews that we will animate
MyViewController *tmpFirstIV=[[MyViewController alloc] init];
tmpFirstIV.view.backgroundColor=[UIColor yellowColor];
tmpFirstIV.view.frame=CGRectMake(10.0f, 10.0f, 100.0f, 100.0f);
self.firstIV=tmpFirstIV;
[self addChildViewController:self.firstIV];
[self.view addSubview:self.firstIV.view];
[self.firstIV didMoveToParentViewController:self.firstIV];
-(void)updateAction:(id) sender{
NSLog(#"Here are some results");
if([self.updateButton.currentTitle isEqualToString:#"update this"]){
[UIView animateWithDuration:6.0f animations:^{ // yeah I know, long animation
self.firstIV.view.frame=CGRectMake(10.0f, 10.0f, 100.0f, 0.0f);
}];
[self.updateButton setTitle:#"make bigger" forState:UIControlStateNormal];
}else{
[UIView animateWithDuration:6.0f animations:^{ // equally long
self.firstIV.view.frame=CGRectMake(10.0f, 10.0f, 100.0f, 100.0f);
}];
[self.updateButton setTitle:#"update this" forState:UIControlStateNormal];
}
}
edit2
doesn't seem to be doing anything... will look into this layer.
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
CGRect viewBounds = self.view.bounds;
self.myLabel.frame = CGRectMake(100.0f,viewBounds.size.height-20.0f,50.0f,50.0f);
}
You are only animating the frame of the yellow box, not the label. The frame of the label will not change just because its superview's frame has changed. You either need to:
Write code to animate both objects when the button is tapped
Write code to automatically move the label when the box is resized, typically by overriding your view controller's - (void)viewWillLayoutSubviews method.
If you're targeting iOS 6 only, you can use the new auto-layout feature. Ray Wenderlich has an excellent tutorial.
Related
I'm trying to figure out if there is a way to test the layout of an iOS view in unit tests when using autolayout. Right now I try to simply initialize the view controller, and then check the frames of the views. However, the frame on each view remains origin=(x=0, y=0) size=(width=0, height=0).
- (void)setUp {
[super setUp];
_viewController = [[AddPlayerViewController alloc] init];
_viewController.view.frame = CGRectMake(0, 0, 320, 460);
[_viewController view];
}
- (void)testViewsAreInsideWindow {
[self checkIfViewIsInsideWindow:_viewController.txtfNewPlayer];
[self checkIfViewIsInsideWindow:_viewController.btnNewPlayer];
[self checkIfViewIsInsideWindow:_viewController.tblPlayers];
}
- (void)checkIfViewIsInsideWindow:(UIView *)view {
CGRect windowFrame = _viewController.view.frame;
CGRect viewFrame = view.frame;
XCTAssertTrue(CGRectContainsRect(windowFrame, viewFrame));
}
I've tried adding
[_viewController.view needsUpdateConstraints];
or
[_viewController.view updateConstraints];
or
[_viewController updateViewConstraints];
or
[_viewController viewWillAppear:YES];
but none of them have helped.
Is it at all possible to get autolayout to run when using XCTest?
Have you tried setNeedsLayout followed by layoutIfNeeded?
You can definitely get layout to run in tests, I do it here, but that doesn't have a view controller, just views.
I believe what you are trying to see is whether elements are visible to the user. You may use isHittable property in XCUIElement.
From documentation https://developer.apple.com/documentation/xctest/xcuielement/1500561-ishittable
isHittable returns true if the element exists and can be clicked, tapped, or pressed at its current location. It returns false if the element does not exist, is offscreen, or is covered by another element.
so there must be something simple I am missing because I just can't figure out how to move my UIView
This is what I have so far
my .m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self buttonLayout];
}
-(void)buttonLayout
{
UIView *theView = [self buttonGroup];
theView.center=CGPointMake(50, 50);
}
My .h file
#property (nonatomic, retain) IBOutlet UIView *buttonGroup;
So this is what I have so far and i just cant seem to get it to move at all
P.S i don't want it animated as i'm getting my layout to move to compensate for different iPhone screen sizes :)
I have not tested this, but it seems like you need to:
A) Remove the view from its parent.
B) Adjust the center of its 'frame', not the view.
C) Re-add the subview.
Like so:
UIView *theView = [self buttonGroup];
CGRect theFrame = theView.frame;
[theView removeFromSuperview];
theFrame.origin.x = 50.0;
theFrame.origin.y = 50.0;
theView.frame = theFrame;
[self.view addSubview:theView];
This should work with autolayout.
As the other poster says, you can't move views around by changing their center or frame when you have auto-layout in effect.
Instead, you have to add a vertical and horizontal constraint that positions the view from the top/left edge, connect an outlet to the constraint, and then change the constant value on the constraint from code.
I had similar problem , but when i tried to change the origin as suggested in first solution , i got "Expression not assignable" error.
so i solved it with below solution in viewDidLoad().
First save its center in some temp variable , then change the centre and add again to its parent view.
CGPoint buttonCenter = self.yourButton.center;
[self.yourButton removeFromSuperview];
buttonCenter.y = buttonCenter.y + 50.0;
self.yourButton.center = buttonCenter;
[self.view addSubview:self.yourButton];
Does anyone know how to achieve the present view controller's view to shrink and put another one over it with a transparency? It is achieved in Tweetbot 3 when you tap on your avatar in the top left on the navigation bar. Should I take a snapshot for example?
In order to achieve this effect you will have to rebuild your view stack from scratch.
So as there is no possibility to change the viewController.view's frame, you'll have to add a kind of container subview a little like this:
#implementation ViewController
#synthesize container;
- (void)viewDidLoad {
[super viewDidLoad];
container = [[UIView alloc] initWithFrame:self.view.frame];
[self.view addSubview:container];
// add all views later to this insted of self.view
// continue viewDidLoad setup
}
Now if you have that, you can animate the shrinking behavior like so:
[UIView animateWithDuration:.5 animations:^{
container.frame = CGRectMake(10, 17, self.view.frame.size.width-20, self.view.frame.size.height-34);
}];
Okay, I assume you are developing for iOS 7, so we'll make use of some new APIs here (for earlier versions there are alternative frameworks). Now since WWDC UIView's got a resizableSnapshotViewFromRect:(CGRect) afterScreenUpdates:(BOOL) withCapInsets:(UIEdgeInsets) method returning a single UIView object.
[UIView animateWithDuration:.5 animations:^{
container.frame = CGRectMake(10, 17, self.view.frame.size.width-20, self.view.frame.size.height-34);
} completion:^(BOOL finished) {
UIView *viewToBlur = [self.view resizableSnapshotViewFromRect:container.frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
}];
If you do not want to rewrite your view management, you can also first take a snapshot of your main view this way, set it to the container and then animate only the image. But remember, you can not interact with the captured view then.
When you've got that, you can download the two category files from this repo (They're from WWDC, so directly from Apple!). Basically, what they do is, they add some cool new methods to the UIView class, of which we'll use applyDarkEffect. I haven't tested this, maybe another method fits your needs better here.
Anyways if we implement that into the block and maybe also add a UIImageView to display the blurred overlay, it should look something like this:
[UIView animateWithDuration:.5 animations:^{
container.frame = CGRectMake(10, 17, self.view.frame.size.width-20, self.view.frame.size.height-34);
} completion:^(BOOL finished) {
UIView *viewToBlur = [self.view resizableSnapshotViewFromRect:container.frame afterScreenUpdates:YES withCapInsets:UIEdgeInsetsZero];
UIImage *image = [viewToBlur applyDarkEffect];
UIImageView *blurredView = [[UIImageView alloc] initWithFrame:self.view.frame];
[self.view addSubview:blurredView];
// optionally also animate this, to achieve an even smoother effect
[blurredView setImage:image];
}];
You can then add your SecondViewController's view on top of the stack, so that it's delegate methods will still be called. The bounce effect of the incoming account view can be achieved via the new UIView animation method animateWithDuration:(NSTimeInterval) delay:(NSTimeInterval) usingSpringWithDamping:(CGFloat) initialSpringVelocity:(CGFloat) options:(UIViewAnimationOptions) animations:^(void)animations completion:^(BOOL finished)completion (more on that in the documentation)
I hope that will help you with your project.
I got this warning:
'UIView' may not respond to 'addSubview:withAnimation:'
The line of code which produce that warning is this:
[self.masterView addSubview:self.detailImage withAnimation:def];
And my relevant code is like this:
ExUIViewAnimationDefinition *def = [[ExUIViewAnimationDefinition alloc] init];
def.type = ExUIAnimationTypeTransition;
def.direction = ExUIAnimationDirectionMoveUp;
[self.masterView addSubview:self.detailImage withAnimation:def];
[def release];
I looked on the UIView documentation, i thought addSubview may be deprecated, but it still like this.
Does any one know how to solve this warning? Thanx in advance.
addSubview is a method UIView will respond to. addSubview:withAnimation: is not a method UIView will respond to.
If you want to add a subview with a fade or something like that, try this:
self.detailImage.alpha = 0.0;
[self.masterView addSubview:self.detailImage];
[UIView animateWithDuration:0.3 animations:^{
self.detailImage.alpha = 1.0;
}];
To add a subview to a parent, call the addSubview: method of the parent view. This method adds the subview to the end of the parent’s list of subviews.
To insert a subview in the middle of the parent’s list of subviews, call any of the insertSubview:... methods of the parent view. Inserting a subview in the middle of the list visually places that view behind any views that come later in the list.
[self.masterView addSubView:self.def];
[def release];
This helped me to animate subview by sliding out from down of border of parent view
-(void) addAnimatadView:(UIView *) animatedView toView:(UIView *)aView {
CGRect frame = animatedView.frame;
float origin = frame.origin.y;
frame.origin.y = aView.frame.size.height;
[animatedView setFrame:frame];
[aView addSubview:animatedView];
frame.origin.y = origin;
[UIView animateWithDuration:0.4 animations:^{[animatedView setFrame:frame];}];
}
Just change frame origins as you want to reach free sliding
I wonder for an iOS app, what is the best way to implement a slide-out panel with a button and slider inside it?
That is, the app will have a small "i" icon at the bottom right corner of the screen that kind of look like:
When that icon is touched, a panel of size about 1/4 of the screen will slide out from the bottom, showing a button, a slider, or possibly some other widgets.
What is the best way to implement it -- using Interface Builder or is it better purely by code, and the object to use for the panel (UIView or any existing widget that has this slide out / slide in function?).
Assuming you are going to use this in multiple locations within your app you would probably want to make it as a reusable component. I would suggest making the slide out screen its own view controller where you could create its view using IB if you need. When the user hits the 'i' you would alloc the slide out view controller, add its view as a subview to your current view controller and animate it in. If you need to communicate between the slide out and your current view controller you could just create a custom delegate protocol on your slide out. I recognize this is making some assumptions but would likely be a clean solution for you.
Creating this by Interface Builder is a pain (anyways, that tool sucks). You should definitely create that in code. It will also be easier to maintain if you can concentrate on the code and don't have to mess with setting mysterious properties in IB only for the view to be displayed at all...
You can try implementing it like this:
#interface SlideOutMenu: UIView {
UIImageView *imgView;
UITapGestureRecognizer *tap;
}
- (id)initWithImage:(UIImage *)img;
#end
#implementation SlideOutMenu
- (id)initWithImage:(UIImage *)img
{
if ((self = [self initWithFrame:CGRectMake(0, someY, 44, 44)]))
{
imgView = [[UIImageView alloc] initWithImage:img];
[self addSubview:imgView];
[imgView release];
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(_tapped)];
[self addGestureRecognizer:tap];
[tap release];
}
return self;
}
- (void)_tapped
{
if (self.frame.origin.x > 1.0) // 1.0: some tolerance b/c of floating point numbers
{
[UIView animateWithDuration:0.3 animations:^{
self.frame = CGRectMake(0, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}];
}
else
{
[UIView animateWithDuration:0.3 animations:^{
self.frame = CGRectMake(120.0, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}];
}
}
#end