How to make iPad rotations more fluid? - ios

I'll show you two methods: the first one "redefineLayout" calculate new positions of elements and the second one is the hook "didRotateFromInterfaceOrientation" after rotation.
- (void)redefineLayout
{
int margin = 20;
int buttonWidth = ((int)self.view.frame.size.width - margin * 4)/3;
[self.buttonNewOrder setFrame:CGRectMake(margin, 20, buttonWidth, 200)];
[self.buttonStatistics setFrame:CGRectMake(margin * 2 + buttonWidth, 20, buttonWidth, 200)];
[self.buttonSincronize setFrame:CGRectMake(margin * 3 + buttonWidth * 2, 20, buttonWidth, 200)];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self redefineLayout];
}
My interface work but after rotations I see elements make a quick and unsightly movement. I'll like to make this movement more fluid. Any suggestion?
My solution:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self redesignLayout:duration];
}
- (void)redesignLayout:(NSTimeInterval)duration {
[UIView animateWithDuration:duration animations:^{
int margin = 20;
int buttonWidth = (((int)self.view.frame.size.width) - margin * 4)/3;
[self.buttonNewOrder setFrame:CGRectMake(margin, 20, buttonWidth, 200)];
[self.buttonStatistics setFrame:CGRectMake(margin * 2 + buttonWidth, 20, buttonWidth, 200)];
[self.buttonSincronize setFrame:CGRectMake(margin * 3 + buttonWidth * 2, 20, buttonWidth, 200)];
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initLayout];
[self redesignLayout:0];
}

Try replacing your didRotateFromInterfaceOrientation call with this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration
{
[UIView animateWithDuration:duration animations:^{
[self redefineLayout];
}];
}

Related

Resize button / image by user's choice

,I would like to give the user the option to select the button size as per the user's choice ... Can I do this please?
mytweak.xm
UIButton *respringButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
respringButton.frame = CGRectMake(self.view.frame.size.width - 250, self.view.frame.size.height / 2 - 80, 60, 60);
[respringButton setImage:[UIImage imageWithContentsOfFile: respring] forState:UIControlStateNormal];
[respringButton setTitle:#"respring" forState:UIControlStateNormal];
[respringButton addTarget:self action:#selector(reboot) forControlEvents:UIControlEventTouchUpInside];
if(style != 1) {
respringButton.tintColor = tintColor;
} else {
if(defaultTheme1 == 0) {
respringButton.frame = CGRectMake(self.view.frame.size.width - 250, self.view.frame.size.height , 60, 60);
} else if (defaultTheme1 == 1) {
respringButton.frame = CGRectMake(self.view.frame.size.width - 250, self.view.frame.size.height , 80, 80);
} else if (defaultTheme1 == 2) {
respringButton.frame = CGRectMake(self.view.frame.size.width - 250, self.view.frame.size.height , 100, 100);
}
}
[respringButton centerVertically];
[myView addSubview:respringButton];
mytweak.h
- (void)centerVertically {
// Spacing between the text and the image
CGFloat spacing = 5.0;
// Lower the text and push it left so it appears centered below the image
CGSize imageSize = self.imageView.frame.size;
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing),- 120.0);
// Raise the image and push it right so it appears centered above the text
CGSize titleSize = self.titleLabel.frame.size;
self.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);
}
#end

iOS - layoutIfNeeded not creating frame in custom UITableViewCell

I have this class here for a custom tableview cell but in awakeFromNib, self.frame is always 0,0,0,0. I tried calling [self layoutIfNeeded] but that has no effect. I need the frame to place c in the right place in the cell. The code definitely runs (I've tried breakpoints), so why isn't it working?
#import "ChangeColourSubjectColourTableViewCell.h"
#import <QuartzCore/QuartzCore.h>
#define COLOUR_HEIGHT_DECIMAL 0.8
#define CORNER_RADIUS 6.0
#implementation ChangeColourSubjectColourTableViewCell
- (void)awakeFromNib {
// Initialization code
[self layoutIfNeeded];
//Colour View
//Size
CGRect rect;
rect.size.height = self.frame.size.height * COLOUR_HEIGHT_DECIMAL; //80% height
rect.size.width = rect.size.height;
//Position
CGFloat gap = self.frame.size.height * (1 - COLOUR_HEIGHT_DECIMAL) / 2;
rect.origin.y = gap;
rect.origin.x = self.frame.size.width - rect.size.width - gap;
UIView *c = [[UIView alloc] initWithFrame:rect];
[c setBackgroundColor:[UIColor blackColor]];
[c.layer setCornerRadius:CORNER_RADIUS];
[self addSubview:c];
_colourView = c;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)setColourViewColour:(UIColor *)colour {
[_colourView setBackgroundColor:colour];
}
#end
The awakeFromNib method is called when the cell is loaded from the xib, but the cell's frame is manage by the tableview.
You should create subviews as variables in method awakeFromNib and override method setFrame/layoutSubviews to layout your subview, the cell.frame in method layoutSubviews is always true.
you can do like this:
- (void)awakeFromNib {
// Initialization code
UIView *c = [[UIView alloc] initWithFrame:CGRectZero];
[c setBackgroundColor:[UIColor blackColor]];
[c.layer setCornerRadius:CORNER_RADIUS];
[self addSubview:c];
_colourView = c;
}
- (void)layoutSubviews {
[super layoutSubviews];
//Colour View
//Size
CGRect rect;
rect.size.height = self.frame.size.height * COLOUR_HEIGHT_DECIMAL; //80% height
rect.size.width = rect.size.height;
//Position
CGFloat gap = self.frame.size.height * (1 - COLOUR_HEIGHT_DECIMAL) / 2;
rect.origin.y = gap;
rect.origin.x = self.frame.size.width - rect.size.width - gap;
_colourView.frame = rect;
}
In iOS6 or later, you can use
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell reloadUI];// you can do like this
}
// cell
- (void) reloadUI {
CGRect rect;
rect.size.height = self.frame.size.height * COLOUR_HEIGHT_DECIMAL; //80% height
rect.size.width = rect.size.height;
//Position
CGFloat gap = self.frame.size.height * (1 - COLOUR_HEIGHT_DECIMAL) / 2;
rect.origin.y = gap;
rect.origin.x = self.frame.size.width - rect.size.width - gap;
_colourView.frame = rect;
}

iOS, create custom popover style UIView

I know there is alot of documentation on this topic, and from what i've read i've learned a lot. My issue here is that i began creating a custom UIView within a viewcontroller & now when i'm trying to make it the UIView its own class, so as to be called & used within a working app, i'm pretty confused as to what i need to do to achieve it.
The overall aim of the class is to be able to select a button one a view controller, which will instantiate the custom view, then display. The visual effect for the user is that they click the button, then a window will animate-appear from the bounds of the button (although the code below uses the positioning of top left currently), then display a combination of switches & labels, along with a back & save button.
You can see from my code that what i need to achieve this process, i've managed to implement into a single function (again, this was from creating it within a viewcontroller). What i'm after now, and can't quite fathom out is for the custom view to be imported, then on the action of the main view controller's button, add the view to self.view.
- (void)makeBox{
//view bounds
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
CGFloat viewHeight = CGRectGetHeight(self.view.frame);
//red box x, y & size
CGFloat startx = (viewWidth / 100) * 10;
CGFloat starty = (viewHeight /100) * 20;
CGFloat width = (viewWidth / 100) * 80;
CGFloat height = (viewHeight /100) * 70;
CGRect view1Frame = CGRectMake(startx, starty, width, height);
//label & switch frame
CGRect labelMR = CGRectMake(10, ((height / 100) * 12), ((width / 100) * 80) - 7.5, 25);
CGRect switchMR = CGRectMake(((width / 100) * 80) + 7.5, ((height / 100) * 11), ((width / 100) * 10), 15);
//this is repeated for 6 other labels & switches
CGRect backButtonR = CGRectMake(5, 5, 50, 35);
CGRect saveButtonR = CGRectMake((width - 50), 5, 50, 35);
if (!self.view1) {
self.switchM = [[UISwitch alloc] initWithFrame:switchMR];
self.switchM.tag = 10;
if(self.monday){ //self.monday refers to a BOOL property of the viewcontroller that this was originally made in
self.switchM.on = true;
} else{
self.switchM.on = false;
}
[self.switchM addTarget:self action:#selector(switched:) forControlEvents:UIControlEventTouchUpInside];
// this switch instantiation process is repeated 6 other times
self.backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.backButton addTarget:self
action:#selector(hideBox)
forControlEvents:UIControlEventTouchUpInside];
[self.backButton setTitle:#"Back" forState:UIControlStateNormal];
self.backButton.frame = backButtonR;
self.saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.saveButton addTarget:self
action:#selector(daysChanged)
forControlEvents:UIControlEventTouchUpInside];
[self.saveButton setTitle:#"Save" forState:UIControlStateNormal];
self.saveButton.frame = saveButtonR;
self.labelM = [[UILabel alloc] initWithFrame:labelMR];
self.labelM.layer.masksToBounds = YES;
self.labelM.layer.cornerRadius = 5;
self.labelM.layer.borderColor = [UIColor blackColor].CGColor;
self.labelM.layer.borderWidth = .5;
self.labelM.text = #" Monday";
self.labelM.backgroundColor = [UIColor whiteColor];
//again - repeated 6 times
//use this starting frame to make the 'popover' appear small at first
CGRect startFrame = CGRectMake(10, 10, 10, 10);
self.view1 = [[UIView alloc] initWithFrame:startFrame];
[self.view addSubview:self.view1];
[UIView animateWithDuration:0.5 animations:^(void){
self.view1.layer.cornerRadius = 10;
self.view1.layer.borderColor = [UIColor blackColor].CGColor;
self.view1.layer.borderWidth = .5;
[self.view1 setBackgroundColor:[UIColor lightGrayColor]];
self.view1.frame = view1Frame;
} completion:^(BOOL finished){
[self.view1 addSubview:self.labelM];
[self.view1 addSubview:self.switchM];
//repeat 6 times for other labels & switches
}];
}
else {
[UIView animateWithDuration:0.3 animations:^() {
self.view1.frame = view1Frame;
} completion:^(BOOL finished) {
self.labelM.frame = labelMR;
self.switchM.frame = switchMR;
//repeated for the other 6
self.backButton.frame = backButtonR;
self.saveButton.frame = saveButtonR;
}];
}
}
-(void) hideBox{
//back button was selected
[UIView animateWithDuration:0.5 animations:^(void){
[self.view1 removeFromSuperview];
self.view1 = nil;
}];
}
I understand that as i'm doing/calling this via code, i need to override either init or initWithFrame:. Do i pass the initWithFrame: the containing view controllers bounds, of which the custom view can be calculated on, and how do i handle the animation?
I've managed to set up the delegate protocol which will notify when the save button is pressed. that part i understand, its just the rest of the parts i'm unclear on.
First some background.
Typically for a popover controller, you have a single transparent root view which contains the popover view itself. This root view is the size of the containing window and usually higher in z-order than all other views. There are two reasons for this:
1) The popover needs to draw on top of all other views. If the root view wasn't on top of all other views, the popover could be clipped and not draw all of its contents.
2) The user usually needs some place to tap and dismiss the popover (effectively a cancel action).
UIPopoverController does this as do some 3rd party popovers. Some even go so far as to use their own UIWindows.
To answer your specific questions:
1) initWithFrame: / setFrame: should be passed a CGRect that is in the coordinate space of the view that will contain the view. In other words, the superview. If you decide to use a transparent root view, the popover (the part that draw on screen) will be passed a CGRect in the coordinate space of the transparent root view.
2) Usually a popover is animated like so:
// set initial frame
popover.frame = CGRectMake(x, y, width, 0);
popover.alpha = 0.0;
[rootView addSubview:popover];
[UIView animateWithDuration:0.5
animations:^{
CGRect frame = popover.frame;
frame.size.height = some-height;
popover.frame = frame;
popover.alpha = 1;
}
];
I actually got it to work! (although i can't promise this is the cleanest or most resourceful way - we'll work on that).
Here's what i did:
Split the makeBox function into 3 function, initWithWidth:andHeight:forView, openBox & updateBoxWithWidth:andHeight.
I used a saveButton delegate method that informs the parent that save button has been pressed.
.h
#protocol BoxDelegate
-(void) daysChanged;
#end
__weak id <BoxDelegate> delegate;
#interface aBox : UIView
#property (nonatomic) BOOL monday;
#property (nonatomic, weak) id <BoxDelegate> delegate;
//property values for the custom view
-(id) initWithWidth: (CGFloat) width andHeight: (CGFloat) height withView: (UIView*) theView;
-(void) updateViewWithWidth: (CGFloat) width andHeight: (CGFloat) height;
-(void) openBox;
#end
.m
#synthesize delegate;
-(id) initWithWidth:(CGFloat)inWidth andHeight:(CGFloat)inHeight withView: (UIView*) theView{
self = [super init];
self.mainView = theView;
//super view bounds
self.viewWidth = inWidth;
self.viewHeight = inHeight;
//red box x, y & size
CGFloat startx = (self.viewWidth / 100) * 10;
CGFloat starty = (self.viewHeight /100) * 20;
self.width = (self.viewWidth / 100) * 80;
self.height = (self.viewHeight /100) * 70;
self.view1Frame = CGRectMake(startx, starty, self.width, self.height);
//label & switch frame
self.labelMR = CGRectMake(10, ((self.height / 100) * 12), ((self.width / 100) * 80) - 7.5, 25);
self.switchMR = CGRectMake(((self.width / 100) * 80) + 7.5, ((self.height / 100) * 11), ((self.width / 100) * 10), 15);
//repeated for the other 6
self.backButtonR = CGRectMake(5, 5, 50, 35);
self.saveButtonR = CGRectMake((self.width - 50), 5, 50, 35);
self.switchM = [[UISwitch alloc] initWithFrame:self.switchMR];
self.switchM.tag = 10;
if(self.monday){
self.switchM.on = true;
} else{
self.switchM.on = false;
}
[self.switchM addTarget:self action:#selector(switched:) forControlEvents:UIControlEventTouchUpInside];
self.labelM = [[UILabel alloc] initWithFrame:self.labelMR];
self.labelM.layer.masksToBounds = YES;
self.labelM.layer.cornerRadius = 5;
self.labelM.layer.borderColor = [UIColor blackColor].CGColor;
self.labelM.layer.borderWidth = .5;
self.labelM.text = #" Monday";
self.labelM.backgroundColor = [UIColor whiteColor];
//repeated for the other 6
self.saveButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.saveButton addTarget:delegate
action:#selector(daysChanged)
forControlEvents:UIControlEventTouchUpInside];
[self.saveButton setTitle:#"Save" forState:UIControlStateNormal];
self.saveButton.frame = self.saveButtonR;
return self;
}
-(void) openBox{
CGRect startFrame = CGRectMake(10, 10, 10, 10);
self.view1 = [[UIView alloc] initWithFrame:startFrame];
[self.mainView addSubview:self.view1];
[UIView animateWithDuration:0.5 animations:^(void){
self.view1.layer.cornerRadius = 10;
self.view1.layer.borderColor = [UIColor blackColor].CGColor;
self.view1.layer.borderWidth = .5;
[self.view1 setBackgroundColor:[UIColor lightGrayColor]];
// repeated for the other 6
self.view1.frame = self.view1Frame;
} completion:^(BOOL finished){
[self.view1 addSubview:self.labelM];
[self.view1 addSubview:self.switchM];
[self.view1 addSubview:self.backButton];
[self.view1 addSubview:self.saveButton];
}];
}
-(void) updateViewWithWidth: (CGFloat) width andHeight:(CGFloat) height{
//re-calculate
self.viewWidth = width;
self.viewHeight = height;
CGFloat startx = (self.viewWidth / 100) * 10;
CGFloat starty = (self.viewHeight /100) * 20;
self.width = (self.viewWidth / 100) * 80;
self.height = (self.viewHeight /100) * 70;
self.view1Frame = CGRectMake(startx, starty, self.width, self.height);
//label & switch frame
self.labelMR = CGRectMake(10, ((self.height / 100) * 12), ((self.width / 100) * 80) - 7.5, 25);
self.switchMR = CGRectMake(((self.width / 100) * 80) + 7.5, ((self.height / 100) * 11), ((self.width / 100) * 10), 15);
self.backButtonR = CGRectMake(5, 5, 50, 35);
self.saveButtonR = CGRectMake((self.width - 50), 5, 50, 35);
[UIView animateWithDuration:0.3 animations:^() {
self.view1.frame = self.view1Frame;
} completion:^(BOOL finished) {
self.labelM.frame = self.labelMR;
self.switchM.frame = self.switchMR;
self.backButton.frame = self.backButtonR;
self.saveButton.frame = self.saveButtonR;
}];
}
And then in our parent viewcontroller
#property aBox *theBox;
...
- (void)viewDidLoad {
[super viewDidLoad];
self.theBox = [[aBox alloc] initWithWidth:self.view.frame.size.width andHeight:self.view.frame.size.height withView:self.view];
[self.theBox openBox];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[self.theBox updateViewWithWidth:self.view.frame.size.width andHeight:self.view.frame.size.height];
}
- (void) daysChanged{
NSLog(#"Days Changed!");
}

Can't find a math formula to resize two views

I'm having a start position like you can see on my sketch. My Map always fills up the whole screen. I just resize my tableview on top of it.
When I click on the map , the tableview resizes like you can see on my seconds sketch the left one. If I then click on the tableview, the tableview resizes.
Now the problem is that I can't find a math function that the 2 things are always equal size. Like I point out on the sketch.
This how I initialize the 2 views:
-(void) initMapView
{
_mapView = [[GoogleMapViewController alloc] init];
[self addChildViewController:_mapView];
_mapView.view.frame = CGRectMake(0, 0, self.view.bounds.size.width , self.view.bounds.size.height);
UILongPressGestureRecognizer * tapGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(expandMap:)];
[_mapView.view addGestureRecognizer:tapGesture];
[self.view addSubview:_mapView.view];
[_mapView didMoveToParentViewController:self];
}
-(void) initPOITableView
{
_poiTableView = [[POITableViewController alloc] init];
[self addChildViewController:_poiTableView];
_poiTableView.view.frame = CGRectMake(0, self.view.bounds.size.height / 2, self.view.bounds.size.width, self.view.bounds.size.height);
UILongPressGestureRecognizer * tapGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(expandTableView:)];
[_poiTableView.view addGestureRecognizer:tapGesture];
[self.view addSubview:_poiTableView.view];
[_poiTableView didMoveToParentViewController:self];
}
And this is my animation function:
- (void) expandTableView:(UITapGestureRecognizer *)sender
{
NSLog(#"ExpandTableView Animation");
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
float cellHeight = [_poiTableView getHeightForIndexPath:indexPath];
NSLog(#"Cell height %f", cellHeight );
float navBarHeight = self.navigationController.navigationBar.frame.size.height;
[UIView animateWithDuration:1.0
delay:0.2
options:0
animations:^{
if(_isStartUpPosition)
{
NSLog(#"StartUp");
_poiTableView.view.frame = CGRectMake(_poiTableView.view.frame.origin.x, _poiTableView.view.frame.origin.y - ( _poiTableView.view.frame.origin.y - navBarHeight - 2 * cellHeight ), _poiTableView.view.frame.size.width, _poiTableView.view.frame.size.height);
_isMapExpanded = NO;
_isStartUpPosition = NO;
}
else if(_isMapExpanded)
{
NSLog(#"TableView is now being expanded");
_poiTableView.view.frame = CGRectMake(_poiTableView.view.frame.origin.x, _poiTableView.view.frame.origin.y - ( _poiTableView.view.frame.origin.y - navBarHeight - 2 * cellHeight ), _poiTableView.view.frame.size.width, _poiTableView.view.frame.size.height);
_isMapExpanded = NO;
}
}
completion:nil];
}
- (void) expandMap:(UITapGestureRecognizer *)sender
{
NSLog(#"ExpandMap Animation");
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
float cellHeight = [_poiTableView getHeightForIndexPath:indexPath];
[UIView animateWithDuration:1.0
delay:0.2
options:0
animations:^{
if(_isStartUpPosition)
{
NSLog(#"StartUp");
_poiTableView.view.frame = CGRectMake(_poiTableView.view.frame.origin.x, _poiTableView.view.frame.origin.y + _poiTableView.view.frame.origin.y - cellHeight, _poiTableView.view.frame.size.width, _poiTableView.view.frame.size.height);
_isMapExpanded = YES;
_isStartUpPosition = NO;
}
else if(!_isMapExpanded)
{
NSLog(#"Map is now being expanded");
_poiTableView.view.frame = CGRectMake(_poiTableView.view.frame.origin.x, _poiTableView.view.frame.origin.y + 2 * _poiTableView.view.frame.origin.y , _poiTableView.view.frame.size.width, _poiTableView.view.frame.size.height);
_isMapExpanded = YES;
}
}
completion:nil];
}
I just can't find the ratio between the two so they could be always the same. On every screen.
EDIT
This is now my map expand
if(!_isMapExpanded)
{
NSLog(#"Map is now being expanded");
_poiTableView.view.frame = CGRectMake(_poiTableView.view.frame.origin.x, _poiTableView.view.frame.origin.y, _poiTableView.view.frame.size.width, _shrankHeight);
_isMapExpanded = YES;
_mapView.view.frame = CGRectMake(_mapView.view.frame.origin.x, _mapView.view.frame.origin.y, _mapView.view.frame.size.width, _expandedHeight);
}
and this is my tableview expand:
if(_isMapExpanded)
{
NSLog(#"TableView is now being expanded");
_poiTableView.view.frame = CGRectMake(_poiTableView.view.frame.origin.x, _poiTableView.view.frame.origin.y, _poiTableView.view.frame.size.width, _expandedHeight);
_mapView.view.frame = CGRectMake(_mapView.view.frame.origin.x, _mapView.view.frame.origin.y, _mapView.view.frame.size.width, _shrankHeight);
_isMapExpanded = NO;
}
What I now have is, that when my tableview is going to expand, it only goes to the half of the screen and not to the top.
#property float shrankHeight;
#property float expandedHeight;
#property float normalHeight;
- (void) calculateHeights
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
// If you're going to use landscape, use width.
normalHeight = (screenBounds.height - 44) / 2.0; // Don't forget the navigation bar, though.
shrankHeight = normalHeight / 3.0;
expandedHeight = normalHeight * 5.0 / 3;
}
After you've calculated the heights, you can use them in your animation block.
For example, if the map is going to expand, you should:
_poiTableView.view.frame = CGRectMake(_poiTableView.view.frame.origin.x, _poiTableView.view.frame.origin.y + 2 * _poiTableView.view.frame.origin.y , _poiTableView.view.frame.size.width, shrankHeight);
And
_mapView.view.frame = CGRectMake(_mapView.view.frame.origin.x, _mapView.view.frame.origin.y + 2 * _mapView.view.frame.origin.y , _mapView.view.frame.size.width, expandedHeight);
You will probably need to change their centres after.

Objective-C - Animate button with blocks?

I have a UIScrollView and a UIButton. I am using blocks to animate them however the button won't animate while the UIScrollView does? Can you not animate a button like this?
Any help would be appreciated greatly!
- (void)viewDidLoad
{
[super viewDidLoad];
draw1 = 0;
scrollView.frame = CGRectMake(0, -200, 768, 200);
[scrollView setContentSize:CGSizeMake(768, 200)];
openMenu.frame = CGRectMake(680, 100, 55, 55);
}
- (IBAction)openMenu:(id)sender {
if (draw1 == 0) {
draw1 = 1;
CGRect optionsDrawer = scrollView.frame;
CGRect optionsButton = openMenu.frame;
optionsDrawer.origin.y += 200;
[UIView animateWithDuration:0.5
animations:^{
scrollView.frame = optionsDrawer;
openMenu.frame = optionsButton;
}];
} else {
draw1 = 0;
CGRect optionsDrawer = scrollView.frame;
CGRect optionsButton = openMenu.frame;
optionsDrawer.origin.y -= 200;
[UIView animateWithDuration:0.5
animations:^{
scrollView.frame = optionsDrawer;
openMenu.frame = optionsButton;
}];
}
}
Ah! I forgot to add the origin to the button as well! Oops!
Fixed with adding to each:
optionsButton.origin.y += 200;

Resources