Temporarily set anchor point of UIView, until after keyframe animation - uiview

Setting the anchor point of my UIView (to 0.5, 1.0) causes all sorts of issues with my layout. Can't I just temporarily change it for as long as I need it to do so?

I ended up moving this down 50% of the view's height, so it looks correct. It's a hack-fix but it works.

Related

UIVIew set anchor to right edge

I have a UIView rectangle that I want to resize the width of programatically. I have that working fine, however when the UIView's width resizes, the left edge stays in place and it is the right edge that moves away/closer. I want the opposite effect of this. I want the right edge to stay fixed and have the UIView resize by moving its left edge. I believe the anchor points are what I should be looking into however i have had no luck
I managed to find UIView.rightAnchor however this doesn't seem to do anything. I'm definitely missing something
Unrelated to bounds, there is a very simple way that you can achieve what you are talking about.
This would simply be through animating two changes to the view's frame simultaneously:
UIView.animate(withDuration: 5, animations: {
self.view1.frame.size.width -= 100
self.view1.frame.origin.x += 100
})
This sample code would create something like this:
Of course you can change the speed of the animation if you want it to be basically instantaneous and such, but this method should work for anything you have in mind.

move UIImageView coordinates using swift

ballImage.frame = CGRectOffset( ballImage.frame, 10, 10)
This will move the ballImage object 10px down and to the right for a brief second before resetting and returning to its original location. I need it to move and stay.
Most of us use AutoLayout these days. If so you can't move the frame because the constraints move the view right back again.
In that case, what you have to do is to add horiztonal and vertical constraints to the view you want to move, connect them to outlets, and then in your code modify the constant value of those constraints and call layoutIfNeeded() to trigger the constraint changes to take effect. If you put the call to layoutIfNeeded inside a UIView animateWithDuration call, the change is animated.

Changing UIView Frame Origin with Autolayout

I'm trying to adjust the origin.y value of a UIView's frame. The problem that I'm running into is that the elements aren't moving with the frame with negative offsets. When I give the origin.y a positive value, it works perfectly: the view drops down, leaving black above, and the elements shift down as well. With a negative value, on the other hand, the view moves correctly, leaving black below, but the elements don't change position. I feel like the problem might be with autolayout, and the elements remaining trapped by the top of the screen, not the top of the UIView.
Any ideas on how to fix this?

autolayout preventing button from moving

I posted a question wondering why my UIImages were staying in the same position as in the interface builder, despite me adding the
self.non_image1_outlet.center = CGPoint(x: 211, y: 10)
self.med_image2_outlet.center = CGPoint(x: 244, y: 10)
self.image3_outlet.center = CGPoint(x: 277, y: 10)
I have switched from using images to buttons now. My issue was that no matter what CGPoints I put in the code above, the buttons would always go back to the same position as in the interface builder. I was not getting my desired position. I wanted the other buttons to move somewhere else when one of them is pressed, but again, auto layout is preventing this. The buttons quickly move to said position but "snap" back to place to the original location placed in the interface builder.
I got downvoted since I had lack of code, but i've figured out it isn't a code issue, rather auto layout. Disabling it from the file inspector, the button moved to where I wanted it to go. However, disabling auto layout messed up my whole main UI and caused a huge mess. Is there anyway to disable auto layout on a specific element rather than the whole project? The reason the button did not go to where the CGPoint specified is due to auto layout locking it in place despite me putting no constraints on it.
EDIT: Or just being able to move the UIButton to where I want it to go with auto layout active.
Any changes you make to center or frame will get reset the next time the auto layout engine does a pass.
To move your button, you either need to:
Move the button by updating the constant value of your button's layout constraints
Remove the layout constraints and set the button's position manually
I fixed the issue by re-adding proper constraints. Everything seems to works fine now.
If you are having the same issue I did, I suggest clearing all the current constraints and slowly adding the proper constraints one of by. Top, left, right, and bottom margin, as well as spacing to any other elements.

subView not animating properly when main View shrinks

My app has a UIView called stepView that needs to grow and shrink, always expanding down and to the right, and always shrinking up and to the left. It has a subview, durationLabel, which runs along the entire width of the stepView and has a fixed height.
When the stepView is animated to grow larger, the label grows properly along with it: the text and the label's background slide to the right in sync with the growing stepView. However, when the stepView is animated to grow smaller, it immediately snaps to its new size, leaving a gap between the stepView's shrinking right edge and its right edge until the animation completes.
The label initialization code in stepView.m:
CGRect duration_frame = CGRectMake(0, 0, self.frame.size.width, HEADER_HEIGHT);
self.durationLabel = [[UILabel alloc] initWithFrame:duration_frame];
[self.durationLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self addSubview:self.durationLabel];
And the animation code:
[UIView animateWithDuration:ANIM_DURATION
animations:^{
[stepView setFrame:newFrame];
}
];
Using bounds/center instead of frame produces the same effect. I've considered using a transform instead, but the code for calculating "newFrame" is somewhat involved, so I'd rather avoid rewriting it if possible. I've also tried manually changing the label's frame in the same animation block, but that simply makes it disappear entirely (possibly because I was trying to animate both a view's frame and its subview's frame in the same animation block?). I've also confirmed stepView and its subviews aren't undergoing any other animations at the same time, although there are animations happening to unrelated views in separate animation blocks.
I'm at a loss for why the label should animate perfectly when the stepView grows but fail to animate at all when it shrinks. I haven't seen anything in the documentation that indicates there would be a difference. Thank you for any help you can provide.
I think this is an issue with UILabel - using another UIView as subview instead of an UILabel the animation works fine also when you shrink.
If I understand correctly what you need to obtain as result, however, I would suggest you to try the following:
don't set the autoresizingMask property
// [self.durationLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
set the stepView clipToBounds property to YES
self.clipToBounds = YES;
This way the UILabel frame will not change but only the part which is actually within the stepView bounds will be visible. I hope this might help.
Update: just done a quick research and found that the question is already answered here resize uiview with UILabel and animate it correctly - the answers provided seem to be aligned to what I've suggested above.

Resources