Recursive method containing UIAnimation block, how to properly release - cocoa-touch

I did a small "loader" that I can stick on the UI when I need it. Much like the UISpinnerView.
It has some logic (I simplified the code in the block for this post) that require it to be recursively called. I do it like this:
- (void) blink {
tick++;
if (tick > kNumberOfLights)
tick = 0;
UIView *lightOne = [self viewWithTag:kLightStartIndex];
UIView *lightTwo = [self viewWithTag:kLightStartIndex+1];
[UIView animateWithDuration:0.5
delay: 0.0
options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseOut
animations:^{
if (tick == 0) {
[lightOne setAlpha:kLightOn];
[lightTwo setAlpha:kLightOff];
} else if (tick == 1) {
[lightOne setAlpha:kLightOff];
[lightTwo setAlpha:kLightOn];
}
}
completion:^(BOOL finished){
[self blink];
}];
}
The method [self blink] is called when the view is added to a super view.
There are no objects retained in the Loader class, so when I remove it in the super view
it is released. The problem is that if the animation block is running when I release the view, the completion block will call a deallocated object and cause the error:
(20380,0xa0a29540) malloc: * mmap(size=2097152) failed (error
code=12)
* error: can't allocate region
In the console and the error:
__[LoaderClass blink]_block_invoke_2
in the Debug Navigator.
How do I make sure to correctly tear down the view when it is removed from the super view?

Overriding release is usually a bad idea, but this case might be an exception to the rule.
Instance variables:
BOOL isBlinking;
BOOL releaseWhenDoneAnimating;
Initialize isBlinking = NO and releaseWhenDoneAnimating = NO.
- (void)release
{
if(!isBlinking) {
[super release];
return;
}
releaseWhenDoneAnimating = YES;
}
- (void) blink {
isBlinking = YES;
if(releaseWhenDoneAnimating) {
[super release];
return;
}
tick++;
if (tick > kNumberOfLights)
tick = 0;
UIView *lightOne = [self viewWithTag:kLightStartIndex];
UIView *lightTwo = [self viewWithTag:kLightStartIndex+1];
[UIView animateWithDuration:0.5
delay: 0.0
options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseOut
animations:^{
if (tick == 0) {
[lightOne setAlpha:kLightOn];
[lightTwo setAlpha:kLightOff];
} else if (tick == 1) {
[lightOne setAlpha:kLightOff];
[lightTwo setAlpha:kLightOn];
}
}
completion:^(BOOL finished){
[self blink];
}];
}

Related

Apply and animate many constraints to outlet collection of uiviews

I have an outlet collection of labels. The labels are in stack views parented by a stack view. When the view loads I'd like to have each label fade in and move slightly to the right one after the other. I can apply the constraint in a loop to offset it. But only one will animate back to the final position.
-(void)viewDidLoad {
[super viewDidLoad];
for (UILabel *lbl in _constructionlabels) {
lbl.alpha = 0.0;
leadingCnst=[NSLayoutConstraint
constraintWithItem:lbl
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:[lbl superview]
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:-25];
[self.view addConstraint:leadingCnst];
}
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
leadingCnst.constant = 0;
[UIView animateWithDuration:0.33 delay:2 options:UIViewAnimationOptionCurveEaseOut animations:^{
for (UILabel *lbl in self->_constructionlabels) {
lbl.alpha = 1.0;
}
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
}
How can I apply constraints to every needed label, and then animate all of them one after the other?
Keep references to each label constraint and start all animations at once, each with a delay.
// Declare array to hold references to constraints
NSMutableArray* _labelConstraints = [NSMutableArray array];
-(void) viewDidLoad {
[super viewDidLoad];
for (UILabel * lbl in _constructionlabels) {
lbl.alpha = 0.0;
NSLayoutConstraint* leadingCnst = [NSLayoutConstraint
constraintWithItem: lbl
attribute: NSLayoutAttributeLeading
relatedBy: NSLayoutRelationEqual
toItem: [lbl superview]
attribute: NSLayoutAttributeLeading
multiplier: 1.0
constant: -25
];
[self.view addConstraint: leadingCnst];
// Add constraint reference
[_labelConstraints addObject: #(leadingCnst)];
}
}
-(void) viewDidAppear: (BOOL) animated {
[super viewDidAppear: animated];
for (i = 0; i < [_constructionlabels count]; i++) {
// Get label
Label* lbl = [_constructionlabels objectAtIndex:i];
// Get constraint
NSLayoutConstraint* labelConstraint = [_labelConstraints objectAtIndex:i];
// Animate
[UIView animateWithDuration: 0.33 delay: i options: UIViewAnimationOptionCurveEaseOut animations: ^ {
lbl.alpha = 1.0;
labelConstraint.constant = 0;
[self.view layoutIfNeeded];
}
completion: ^ (BOOL finished) {}
];
}
}
Note: This is just a proof of concept - you may want to refactor the code.
(It's been a while since I wrote ObjC, if you let me know any mistakes I'll correct them.)
You will need to put a recursive function to animate each label one by one without for loop. Add completion block in the animation.
func animate(_ label: UILabel, completion: #escaping () -> Void) {
UIView.animate(withDuration: 1, animations: {
// add animation here
}, completion: { _ in
completion()
})
}
let labelCollection = [UILabel]()
var index = 0
func startAnimation() {
animate(labelCollection[index], completion: {
if self.index < self.labelCollection.count - 1 {
self.index = self.index + 1
self.startAnimation()
}
})
}

Synchronise all animations on collection-view cells

I am working on a project where I need to animate certain collection view cell background color to a flashing state. I am using the code below on my collectionviewcell subclass:
- (void) fadeInAnimation{
if (!self.animationRunning) {
if ([self canRunAnimation]) {
typeof(self) __weak weakSelf = self;
self.nameLabel.textColor = [UIColor blackColor];
self.animationRunning = true;
[UIView animateWithDuration:1.0
delay:0.0
options: UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction animations:^{
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:1.0];
}
completion:^(BOOL finished) {
[weakSelf fadeOutAnimation];
}];
}
else {
[self.layer removeAllAnimations];
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:1.0];
}
}
}
- (void) fadeOutAnimation {
typeof(self) __weak weakSelf = self;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction animations:^{
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:0.65];
}
completion:^(BOOL finished) {
weakSelf.animationRunning = NO;
[weakSelf fadeInAnimation];
}];
}
My problem is that even though this code works, I think it is not very efficient and needs cleaning up. Also what I need, is that, all animations currently running should be synchronised in flashing however my code makes cells fade in and out in different times. I have a refresh method that refreshes UI based on the results on my API call every few seconds. Any ideas how I could synchronise currently running UIAnimations? I hope I have been clear in my question.
I fixed my problem by going through my view's subviews and capturing all collectionview cells on my multiple subviews and once I had them, I set their background color with alpha component gradually.. here's part of my solution inspired by Armand's answer in this question:
Animation UITableViewCell backgroundcolor
- (void) animateCellFromView:(UIView*)subview {
if ([subview isKindOfClass:[PageCollectionItemCell class]]) {
PageCollectionItemCell *cell = (PageCollectionItemCell*)subview;
if ([cell canRunAnimation]) {
cell.backgroundColor = [cell.backgroundColor colorWithAlphaComponent:self.alpha];
}
}
}

How to bring View to top of UIView Hierarchy

My app currently has a search bar and generates a holder view of results dropping down from the search bar. However, the ResultContainerView is behind my other Views. I am wondering how to bring that search bar container view to the top of the UIView hierarchy. I tried using [super bringSubviewToFront:_searchResultsHolderView];
but that did not work. Here is my method for when the searchbar is tapped.
- (void)_expandSearchResults
{
CCLogVerbose (#"");
_searchResultsHolderView.alpha = 0.0;
_searchResultsHolderView.hidden = NO;
[_searchResultsHolderView setFrameHeight:10];
[UIView beginAnimations:#"expandSearchResults" context:nil];
[UIView setAnimationDuration:kAnimationDuration_SearchResults];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector (searchAnimationDidStop:finished:context:)];
_searchResultsHolderView.alpha = 1.0;
[_searchResultsHolderView setFrameHeight:_searchResultsHt];
[super bringSubviewToFront:_searchResultsHolderView];
[UIView commitAnimations]; // start animation
}
And this is my reloadData method:
- (void)_reloadSearchResults:(NSString *)searchS
{
CCLogVerbose (#"");
if ([searchS length] == 0) {
_searchResults = nil;
[_searchResultsTableView reloadData];
return;
}
NSAssert (self.patientService != nil, #"The Patient Service is invalid");
[self.patientService searchPatients:searchS
expandType:AllPatientExpansions
success:^(NSArray *list) {
_searchResults = list;
[_searchResultsTableView reloadData];
}
failure:^(NSError *error) {
CCLogDebug (#"Failed: %#", error);
}];
}
[self.superview bringSubviewToFront:self];
[self bringSubviewToFront:_searchResultsHolderView];
First you need to bring self to the top of the stack. After that you can call bringSubViewToFront and bring your _searchResultsHolderView to the front.

iAdSuite bug leaves white space when ad disappears

I'm trying to incorporate the iAdSuite tab bar view implementation in my app and I'm seeing the same problem in the suite and my app. When the ad appears, my content's view is getting properly resized and the ad appears correctly. When the ad then disappears, it leaves behind white space where it was located. However, I've confirmed that my content view does get resized back to its original height and it gets drawn down to its original bounds. You just can't see the part where the ad was. I've made sure every view gets a layoutIfNeeded and just about everything else I can think of to no avail. Any thoughts?
Edit: I've figured out what the problem is. Apple's example apparently adds _bannerView to self.view every time showBannerView: is called but never removes the view. That still doesn't make complete sense since the banner view is being moved offscreen, but removing it does solve the white space problem. My solution is as follows, but if anyone has a more elegant way, let me know.
- (void)layoutAnimated:(BOOL)animated {
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect contentFrame = self.view.bounds;
contentFrame.origin = CGPointMake(0.0, 0.0);
CGRect bannerFrame = _bannerView.frame;
if (_bannerView.bannerLoaded) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
_contentView.frame = contentFrame;
[_contentView layoutIfNeeded];
_bannerView.frame = bannerFrame;
}
completion:^(BOOL finished) {
if (!_bannerView.bannerLoaded) {
[_bannerView removeFromSuperview];
_bannerView=nil;
}
}];
}
- (void)showBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
_bannerView = bannerView;
if (![self.view.subviews containsObject:_bannerView])
[self.view addSubview:_bannerView];
[self layoutAnimated:animated];
}
- (void)hideBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
[self layoutAnimated:animated];
}
I had the same problem. Removing the bannerview from the super view in the hideBannerView delegate method seems to have solved it.
- (void)hideBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
[self layoutAnimated:animated];
[_bannerView removeFromSuperview];
_bannerView = nil;
}
Thanks for this question and answer, I was pulling hairs with this one.
I changed the damn code like this and now the hiding animation works. I wonder why apple publishes buggy sample code...
- (void)layoutAnimated:(BOOL)animated hide:(BOOL)hide
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect contentFrame = self.view.bounds;
CGRect bannerFrame = _bannerView.frame;
if (!hide) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
contentFrame.size.height += _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
_contentView.frame = contentFrame;
[_contentView layoutIfNeeded];
_bannerView.frame = bannerFrame;
} completion:^(BOOL finished) {
if (hide) {
[_bannerView removeFromSuperview];
_bannerView=nil;
}
}];
}
- (void)showBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
_bannerView = bannerView;
[self.view addSubview:_bannerView];
[self layoutAnimated:animated hide:NO];
}
- (void)hideBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
[self layoutAnimated:animated hide:YES];
}

How to avoid having duplicated code between animated (completionBlock) and non-animated code

I have a question I have asked myself many time. Let's look at the example below :
if (animated) {
[UIView animateWithDuration:0.3 animations:^{
view.frame = newFrame;
} completion:^(BOOL finished) {
// same code as below
SEL selector = #selector(sidePanelWillStartMoving:);
if ([currentPanningVC conformsToProtocol:#protocol(CHSurroundedViewDelegate)] &&
[currentPanningVC respondsToSelector:selector]) {
[(id)self.currentPanningVC sidePanelWillStartMoving:self.currentPanningVC];
}
if ([centerVC conformsToProtocol:#protocol(CHSurroundedViewDelegate)] &&
[centerVC respondsToSelector:selector]) {
[(id)centerVC sidePanelWillStartMoving:self.currentPanningVC];
}
}];
}
else {
view.frame = newFrame;
// same code as before
SEL selector = #selector(sidePanelWillStartMoving:);
if ([currentPanningVC conformsToProtocol:#protocol(CHSurroundedViewDelegate)] &&
[currentPanningVC respondsToSelector:selector]) {
[(id)self.currentPanningVC sidePanelWillStartMoving:self.currentPanningVC];
}
if ([centerVC conformsToProtocol:#protocol(CHSurroundedViewDelegate)] &&
[centerVC respondsToSelector:selector]) {
[(id)centerVC sidePanelWillStartMoving:self.currentPanningVC];
}
}
The code in the completion block and the non-animated code block is the same. And this is often like this, I means the results of both are the same, except, one was animated.
This really bothers me to have two blocks of code that are exactly the same, how can I avoid that please ?
Thanks!
Create block variables for your animation and completion code, and invoke them yourself in the non-animated case. For example:
void (^animatableCode)(void) = ^{
view.frame = newFrame;
};
void (^completionBlock)(BOOL finished) = ^{
// ...
};
if (animated) {
[UIView animateWithDuration:0.3f animations:animatableCode completion:completionBlock];
} else {
animatableCode();
completionBlock(YES);
}
create block object and use it both places!.
void (^yourBlock)(BOOL finished);
yourBlock = ^{
// same code as below
SEL selector = #selector(sidePanelWillStartMoving:);
if ([currentPanningVC conformsToProtocol:#protocol(CHSurroundedViewDelegate)] &&
[currentPanningVC respondsToSelector:selector]) {
[(id)self.currentPanningVC sidePanelWillStartMoving:self.currentPanningVC];
}
if ([centerVC conformsToProtocol:#protocol(CHSurroundedViewDelegate)] &&
[centerVC respondsToSelector:selector]) {
[(id)centerVC sidePanelWillStartMoving:self.currentPanningVC];
}
}
In your code,
if (animated) {
[UIView animateWithDuration:0.3 animations:^{
view.frame = newFrame;
} completion:yourBlock];
}
else {
yourBlock();
}