I added about 500 views to my viewController.view.
This action took about 5 seconds on target.
Now I want the screen to refresh after each subview I'm adding, so the user will see them appears one by one on screen.
I tried this in my viewController:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
for(int i=0; i<500; i++)
{
//...Create aView
[self.view addsubview:aView];
[self.view setNeedsDisplay];
}
}
I run it and nothing happened for 5 seconds then all views appeared at once.
I made sure that [self.view setNeedsDisplay] called from the main thread context.
Any idea how to make those subviews appear one by one?
I found a simple solution. I added a new property to my viewController - 'subviewsCount' witch was initialised to 500. then called the following method from viewDidLoad:
-(void) addSubviewsToMotherView
{
self.subviewsCount -=1;
if (self.subviewsCount >= 0)
{
[UIView animateWithDuration:0.0
delay:0.0
options:UIViewAnimationOptionLayoutSubviews
animations:^{
[self methodToAddSubview];
}
completion:^(BOOL finished){
[self addSubviewsToMotherView];
}
];
}
}
Related
In my project there's a ViewController which contains a few subviews(e.g. buttons).
It shows/hides those buttons, always with animation.
It has an interface like this:
#interface MyViewController: UIViewController
- (void)setMyButtonsVisible:(BOOL)visible;
#end
And an implementation looks like this:
- (void)setMyButtonsVisible:(BOOL)visible
{
if( visible )
{
// "show"
_btn1.hidden = NO;
_btn2.hidden = NO;
[UIView animateWithDuration:0.2 animations:^{
_btn1.alpha = 1.0;
_btn2.alpha = 1.0;
}];
}
else
{
// "hide"
[UIView animateWithDuration:0.2 animations:^{
_btn1.alpha = 0.0;
_btn2.alpha = 0.0;
} completion:^(BOOL finished) {
_btn1.hidden = YES;
_btn2.hidden = YES;
}];
}
}
When [_myVC setMyButtonsVisible:NO] is called, and then after some time ( > 0.2s) [_myVC setMyButtonsVisible:YES] is called, everything is OK.
However, it setMyButtonsVisible:YES is called immediately after ( < 0.2s) setMyButtonsVisible:NO, the animations overlap and setMyButtonsVisible:NO callback is called the last.
I've tried to change "hide" duration to 0.1, it doesn't help - after "hide(0.1)"+"show(0.2)" calls, "hide" callback is called after the "show" callback and my buttons are not visible.
I've added a quick-fix by caching the visible param and checking in "hide" completion handler if the state should be !visible.
My questions are:
Why the first animation completion handler is called the last if animations overlap?
What are better approahes to discard a previous "overlapping" animation?
Check the finished flag on completion:
if (finished) {
_btn1.hidden = YES;
_btn2.hidden = YES;
}
I'm trying to implement a drawer UIContainerView that allows the user to expand and contract it to reveal information. I have the container view set up and the button to "expand" is working fine.
However, once the view is 'expanded' it no longer responds to touch. It has a text view and two buttons. Even more; when I touch the screen it registers as a tap on the collectionView behind it, and NOT the buttons that I'm supposed to be able to interact with.
This is the code handling the parent / child relationship with the container view:
// Parent View Controller.h
{
UIViewController *child1;
profileDescriptionViewController *profileDescription;
}
// Parent View Controller.m
- (void)viewDidLoad
{
child1 = [self.storyboard instantiateViewControllerWithIdentifier:#"profileDescription"];
[child1 didMoveToParentViewController:self];
}
// Child View Controller.m
- (void)viewDidLoad
{
_profileDescription.text = [[dataHandler getAthlete] profileDescriptionString];
_isExposed = NO;
_frame = self.view.frame;
_height = _frame.size.height;
_yOrigin = _frame.origin.y;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)profileExpand:(id)sender
{
if (_isExposed == NO) {
[UIView animateWithDuration:0.5 animations:^
{
//0, 487, 320, 32
[self.view setFrame:CGRectMake(_frame.origin.x, _yOrigin - 100, 320, _yOrigin + 150)];
}
completion:^(BOOL finished)
{
[self.view setUserInteractionEnabled:YES];
_isExposed = YES;
}];
}
/* else
{
[UIView animateWithDuration:0.5 animations:^
{
[self.view setFrame:CGRectMake(_frame.origin.x, _yOrigin + 100, 320, _yOrigin - 150)];
}
completion:^(BOOL finished)
{
_isExposed = NO;
}];
}*/
}
I just want the view to respond to touch events. It's not being placed behind any UI elements on the parentViewController but I can't select either button at the top or the description, and instead it interacts with elements behind it.
Anyone have any ideas as to why this would be? In storyboard its set up like a regular container view.
I met a similar problem, the reason is the super view is not big enough, so after expand the subview, you can see it, but it not responding to any touch event. You can set the clipsToBound of the super view to NO to check it. Hope this helps.
I have a messaging screen I am creating and I am almost done it. I built most of the views with nib files and constraints. I have one small bug however where I can visually see some of the cells laying themselves out when the keyboard dismisses because of the requirement to call [self.view layoutIfNeeded] in an animation block involving a constraint. Here is the problem:
- (void)keyboardWillHide:(NSNotification *)notification
{
NSNumber *duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey];
[UIView animateWithDuration:duration.doubleValue delay:0 options:curve.integerValue animations:^{
_chatInputViewBottomSpaceConstraint.constant = 0;
// adding this line causes the bug but is required for the animation.
[self.view layoutIfNeeded];
} completion:0];
}
Is there any way around directly calling layout if needed on the view since this also causes my collection view to lay itself out which makes the cells layout visually on screen sometimes.
I tried everything I can think of but it I can't find a solution to the bug fix. I have already tried calling [cell setNeedLayout]; in every location possible, nothing happens.
How about this?
In your UITableViewCell implement a custom protocol called MYTableViewCellLayoutDelegate
#protocol MYTableViewCellLayoutDelegate <NSObject>
#required
- (BOOL)shouldLayoutTableViewCell:(MYTableViewCell *)cell;
#end
Create a delegate for this protocol
#property (nonatomic, weak) id layoutDelegate;
Then override layoutSubviews on your UITableViewCell:
- (void)layoutSubviews {
if([self.layoutDelegate shouldLayoutTableViewCell:self]) {
[super layoutSubviews];
}
}
Now, in your UIViewController you can implement the shouldLayoutTableViewCell: callback to control whether the UITableViewCell gets laid out or not.
-(void)shouldLayoutTableViewCell:(UITableViewCell *)cell {
return self.shouldLayoutCells;
}
Modify your keyboardWillHide method to disable cell layout, call layoutIfNeeded, and restore the cell layout ability in the completion block.
- (void)keyboardWillHide:(NSNotification *)notification {
NSNumber *duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey];
self.shouldLayoutCells = NO;
[UIView animateWithDuration:duration.doubleValue delay:0 options:curve.integerValue animations:^{
_chatInputViewBottomSpaceConstraint.constant = 0;
[self.view layoutIfNeeded];
} completion:completion:^(BOOL finished) {
self.shouldLayoutCells = NO;
}];
}
I can't really test this since you didn't provide sample code, but hopefully this will put you on the right path.
I'm trying to build an animation around a UIButton. The UIButton has a UIImageView that contains an image that I'd like to shrink when the UIButton is held down and then when the UIButton is let go, I'd like to play a separate animation that does a bounce.
The issue I'm experiencing right now is that the 2nd part of the animation doesn't seem to play if I press down and then up very quickly. If I press and hold (wait for the first animation to finish), then let go, it seems to work fine.
Here's the relevant code:
-(void)pressedDown
{
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.heartPart.layer.transform = CATransform3DMakeScale(0.8, 0.8, 1);
} completion:nil];
}
-(void)pressedUp
{
[UIView animateWithDuration:0.8
delay:0.0
usingSpringWithDamping:20
initialSpringVelocity:200
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.heartPart.layer.transform = CATransform3DIdentity;
}
completion:nil];
}];
}
In my ViewDidLoad I add the following:
[self.heartButton addTarget:self
action:#selector(pressedUp)
forControlEvents:UIControlEventTouchUpInside];
[self.heartButton addTarget:self
action:#selector(PressedDown)
forControlEvents:UIControlEventTouchDown];
Any idea how I can get the two animations to sequence and not interrupt each other even on a quick press?
Here's a potential plan of action. Keep two global variables, BOOL buttonPressed and NSDate *buttonPressDate. When the button is pressed, you should set buttonPressed to true and set buttonPressDate to the current date. Now in the touch up method, you would set buttonPressed to false and check whether the time interval between buttonPressDate and the current date is greater than the duration of the animation. If it is, then run the touch up animation; if not, return. In the completion block of the touch down method, you would check if the button was still pressed. If it is, then do nothing; if it's not pressed anymore, then run the touch up animation.
The effect you'll get using this approach should be the following: if you tap the button quickly, it will run the full 0.2-second shrink animation and then run the enlarge animation in sequence.
Now, if you don't want the touch down animation to run in the case of a quick touch, you should probably delay the first animation and check if the button is still pressed when you start it. If it was a quick touch, you would run a modified animation that covered both the touch down and touch up phases.
You could try out the following :-
//Set only one target with TouchUpInside.
[self.heartButton addTarget:self
action:#selector(pressedUp)
forControlEvents:UIControlEventTouchUpInside];
-(void)pressedDown
{
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.heartPart.layer.transform = CATransform3DMakeScale(0.8, 0.8, 1);
}
completion:
[self pressedUp];
//Or
[self performSelector:#selector(pressedUp) withObject:self afterDelay:0.5];
];
}
-(void)pressedUp
{
[UIView animateWithDuration:0.8
delay:0.0
usingSpringWithDamping:20
initialSpringVelocity:200
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.heartPart.layer.transform = CATransform3DIdentity;
}
completion:nil];
}];
}
This way as first animation is completed then next animation will start But my concern is that it won't look appropriate and it would be a bit long animation to show to a user. Rest it's upto your app design and what all it is going to achieve with it.
I assume you are familiar with the concept of operations. If not then NSOperations allow you to keep your code modular and enable you to set the order of execution.
You could create a custom subclass of NSOperation and run animation within its execution block, then add operations with animations on queue each time user interacts with the button.
Apart from documentation from Apple, there is a great example of how to subclass NSOperation available on Github:
https://github.com/robertmryan/AFHTTPSessionOperation/blob/master/Source/AsynchronousOperation.m
Based on that "blueprint" for your custom operations, it's very trivial to achieve what you want, i.e.:
#interface PressDownAnimationOperation : AsynchronousOperation
- (instancetype)initWithView:(UIView *)view;
#end
#implementation PressDownAnimationOperation {
UIView *_view;
}
- (instancetype)initWithView:(UIView *)view {
self = [super init];
if(self) {
_view = view;
}
return self;
}
- (void)main {
// dispatch UIKit related stuff on main thread
dispatch_async(dispatch_get_main_queue(), ^{
// animate
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
_view.layer.transform = CATransform3DMakeScale(0.8, 0.8, 1);
} completion:^(BOOL finished){
// mark operation as finished
[self completeOperation];
}];
});
}
#end
Now in order to run animations you need to create NSOperationQueue. You can keep it within your view controller, i.e.:
#implementation ViewController {
NSOperationQueue *_animationQueue;
}
- (void)viewDidLoad {
[super viewDidLoad];
_animationQueue = [[NSOperationQueue alloc] init];
// limit queue to run single operation at a time
_animationQueue.maxOperationCount = 1;
}
#end
Now when you want to chain animations, you simply create new operation, add it to queue and that's it, i.e.:
- (void)pressedDown {
NSOperation *animationOperation = [[PressDownAnimationOperation alloc] init];
[_animationQueue addOperation:animationOperation];
}
If user taps too fast and you notice that your operation queue is clogged with animations, you can simply cancel all animations before adding new one, i.e.:
[_animationQueue cancelAllOperations];
I have a UIScrollView called scroller. It has subviews which are custom UIButtons..I try to print the number of subviews before and after adding like as follows
NSLog(#"Adding %d b4 %d",buttonno, [[self.scroller subviews] count]);
[self.scroller addSubview:menuBtn];
NSLog(#"Adding %d after %d",buttonno, self.scroller.subviews.count);
The subview is not showing up. But the number of subviews increments for example the output of this is like
2014-03-11 17:53:49.863 PC2[4519:60b] Adding 1 b4 10
2014-03-11 17:53:49.872 PC2[4519:60b] Adding 1 after 11
But when I try to print the number of subviews using a test button which runs the code
NSLog(#"Buttons subviews: %d",self.scroller.subviews.count);
I can see that no subview is added..I can also see that no subview is added when I see the variables in the debugger.
btw I'm adding buttons based on timer [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:#selector(checkForTiles) userInfo:nil repeats:YES];
Update:: The code to add subview
if (!alreadyExists)
{
NSLog(#"Adding %d b4 %d",buttonno, [[self.scroller subviews] count]);
//AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
pccButtonViewController *menuBtn=nil;
menuBtn=[pccButtonViewController alloc ];
menuBtn = [pccButtonViewController buttonWithType:UIButtonTypeCustom];
menuBtn.no=buttonno;
menuBtn.slotNo=buttonCount;
menuBtn.frame = [[slotFrames objectAtIndex:menuBtn.slotNo] CGRectValue];
[menuBtn setBackgroundImage:[UIImage imageNamed:[[NSString alloc] initWithFormat:#"L%02d_Tile.jpg",buttonno]] forState:UIControlStateNormal];
[menuBtn addTarget:self action:#selector(miniButtons:) forControlEvents:UIControlEventTouchUpInside];
menuBtn.no=buttonno;
menuBtn.alpha=0.0f;
[self.scroller addSubview:menuBtn];
NSLog(#"Adding %d after %d",buttonno, self.scroller.subviews.count);
//NSLog(#"Subview Added %d btn no:%#",buttonno,[slotFrames objectAtIndex:menuBtn.slotNo]);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
//self.menuBtn.frame = CGRectMake(8, 10, 50, 60);
menuBtn.alpha=1.0f;
[UIView commitAnimations];
[buttons addObject:menuBtn];
buttonCount++;
}
buttonclicked=0;
[self.scroller setContentSize:CGSizeMake([self.scroller bounds].size.width,(ceil(buttonCount/tilesPerRow)+1)*[self.scroller bounds].size.width/tilesPerRow/2*3)];
appdelegate.Sharevar.vidsyncButtons=self.scroller.subviews.copy;//Used to maintain State when the view is presented for the second time
This works fine when the View runs for the first time.The problems arise when it is run for the second time..I remove the view from appdelegate like this
-(void)removeAnyView
{
NSLog(#"Removing");
if (viewonScreen)
{
viewonScreen=false;
[self.inPort prepareToBeDeleted];
self.inPort=Nil;
[self.window.rootViewController dismissViewControllerAnimated:NO completion:^{[self presentView];}];
}
}
It's been a long time.. I forgot about this question.. The error was caused by not invalidating the NSTimer.. Got to be careful with these..
Any updates to the UI have to be on the main thread, which your NSTimer won't be, so you need to do something like this...
dispatch_sync(dispatch_get_main_queue(), ^{
[self.scroller addSubview:menuBtn];
});
Change this
menuBtn=[pccButtonViewController alloc ];
to this
menuBtn=[[pccButtonViewController alloc ] init];
From documenation:
buttonWithType:
This method is a convenience constructor for creating button objects
with specific configurations. If you subclass UIButton, this method
does not return an instance of your subclass. If you want to create an
instance of a specific subclass, you must alloc/init the button
directly.