Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView animation causes a UIButton to jump

UI is programmatically generated in my app. I have few buttons and texboxes in the UI. I have developed a custom uidatapicker object, which pops up from the top and animates to the middle of the screen. When uidatapicker pops up I draw another UIView(called helper view) with the size of the screen so all other uiobjects in the screen except uidatepicker become disabled. But when the UIDatePicker is animating a button in the UI jumps to another location. Also I have three buttons in my UI. It happens with only one button(one UIButon in the UIView). Other two buttons are ok. Also there is no significant difference between those buttons except the button text.

  • I removed the earlier described view(helper view), but still the problem is occurring.
  • I need to know why this is occurring how to prevent it.
  • Also I have lot of other pages which works fine.

The code

-(void)openEditDateTime:(UIDatePickerMode) mode {
    if ([clientView viewWithTag:9]) {
        [self cancelPressed];
    }
    CGRect toolbarTargetFrame = CGRectMake((clientView.frame.size.width/2)-160, (clientView.frame.size.height/2)+91, 320, 44);
    CGRect datePickerTargetFrame = CGRectMake((clientView.frame.size.width/2)-160, (clientView.frame.size.height/2)-125, 320, 216);

    backgroundView = [[UIView alloc] initWithFrame:clientView.bounds];
    backgroundView.alpha = 0;
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.tag = 9;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelPressed:)];
    [backgroundView addGestureRecognizer:tapGesture];
    [clientView addSubview:backgroundView];
    [self bringToFront:backgroundView];

    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.bounds.size.height+44, 320, 216)];
    datePicker.tag = 10;
    datePicker.datePickerMode = mode;
    [clientView addSubview:datePicker];
    [clientView bringSubviewToFront:datePicker];
    [datePicker becomeFirstResponder];
    [self bringToFront:datePicker];
    if(date != nil){
        datePicker.date = date;
    }

    toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.bounds.size.height, 320, 44)];
    toolBar.tag = 11;
    toolBar.barStyle = UIBarStyleBlackTranslucent;
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelPressed:)];
    [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, cancelButton, nil]];
    [clientView addSubview:toolBar];
    [clientView bringSubviewToFront:toolBar];

    [UIView beginAnimations:@"MoveIn" context:nil];
    toolBar.frame = toolbarTargetFrame;
    datePicker.frame = datePickerTargetFrame;
    backgroundView.alpha = 0.5;
    [UIView commitAnimations];
}


- clientView is a UIScrollView.
- backgroundView is the helper view described earlier.

This is how I add buttons.
I wil put only a part of the button rendering code as putting all the code is unnecessary and it has lot other dependencies as well.

-(RenderedElement*)renderElement:(NSObject*)element:(ParentView*)parent:(PageView*)page:(Page*)modelPage:(RenderedElement*)parentRenderedElement {

    UIButton *buttonView = nil;
    Button *templateElement = nil;
    buttonView = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonView setLineBreakMode:UILineBreakModeWordWrap];        
    [buttonView addTarget:parent action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [parent addSubview:buttonView];
}


UPDATE
When I change the order of rendering and if I render the buttons first the jumping effect is not happening. The UI works fine. It temporally solves the problem. But I want find the reason and have a better solution.

like image 922
deltaaruna Avatar asked Oct 24 '13 11:10

deltaaruna


1 Answers

Maybe you can add

[UIView setAnimationBeginsFromCurrentState:YES];

after

[UIView beginAnimations:@"MoveIn" context:nil];
like image 180
Tancrede Chazallet Avatar answered Oct 15 '22 05:10

Tancrede Chazallet