Subview not moving along with its superview - ios

I've done this a million times before but finding myself confused now.
When you animate a container view to change its position, its subviews move along with it don't they? You don't change subviews' frames you just change superview's frame because all subviews' positions inside their superview don't change.
But for some reason this time the subviews would not move along with their superview, it'd stay stuck at its original position.
- (void)writeCommentTapped{
UIView *writeCommentView = [[UIView alloc]init];
writeCommentView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview: writeCommentView];
[self.view bringSubviewToFront:self.navView];
self.writeCommentTextView = [[UITextView alloc]init];
self.writeCommentTextView.backgroundColor = [UIColor blackColor];
[writeCommentView addSubview:self.writeCommentTextView];
[self.writeCommentTextView becomeFirstResponder];
self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;
writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
self.writeCommentTextView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
self.writeCommentTextView.center = writeCommentView.center;
[UIView animateWithDuration:0.15
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);
} completion:^(BOOL finished){
if (finished) {
}
}
];
}

Remove self.writeCommentTextView.center and set calculated frame for center
-(void)writeCommentTapped{
UIView *writeCommentView = [[UIView alloc]init];
writeCommentView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview: writeCommentView];
[self.view bringSubviewToFront:self.navView];
self.writeCommentTextView = [[UITextView alloc]init];
self.writeCommentTextView.backgroundColor = [UIColor blackColor];
[writeCommentView addSubview:self.writeCommentTextView];
[self.writeCommentTextView becomeFirstResponder];
self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;
writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);
self.writeCommentTextView.frame = CGRectMake(40/2, 100/2, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
// self.writeCommentTextView.center = writeCommentView.center;
[UIView animateWithDuration:0.15
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);
} completion:^(BOOL finished){
if (finished) {
}
}
];
}
OR
self.writeCommentTextView.center = CGPointMake(writeCommentView.frame.size.width/2.0, writeCommentView.frame.size.height/2.0);

Related

Display UILabel in UIView programatically or using Storyboard

I have added a UIView and now, i want to show a UILabel. However, it does not get displayed. Can someone help me out please ?
- (IBAction)infoButtonClicked:(id)sender {
myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5, 10, self.view.frame.size.width, 100)];
[myView addSubview:label];
[self.view addSubview: myView];
[UIView animateWithDuration:0.3/1.5 animations:^{
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
myView.transform = CGAffineTransformIdentity;
label.text=#"The title";
}];
}];
}];
//The setup code to detect single touch
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(oneTap:)];
[myView addGestureRecognizer:singleFingerTap];
}
try this
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5, 10, myView.frame.size.width, 100)];
label.text = #"sample TExt";
label.numberOfLines = 1;
label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
label.adjustsFontSizeToFitWidth = YES;
label.adjustsLetterSpacingToFitWidth = YES;
label.minimumScaleFactor = 10.0f/12.0f;
label.clipsToBounds = YES;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
[myView addSubview:label];

UIView animation not work

I'd like to make settingView on myView display and disappear with animation.
I call -showSettingView
if (!self.settingView) : the animation correctly work.
But, else if (self.myView) : settingView disappear WITHOUT animation.
How do I fix it to make settingView disappear with animation?
#property (nonatomic, strong) UIView *myView;
#property (nonatomic, strong) UIView *settingView;
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem* settingButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(showSettingView)];
self.navigationItem.rightBarButtonItem = settingButton;
}
- (void)showSettingView
{
self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, 568, 320, 480)];
self.myView.opaque = NO;
self.myView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.0f];
[self.view addSubview:self.myView];
if (!self.settingView) {
self.settingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
self.settingView.backgroundColor = [UIColor blackColor];
[self.myView addSubview:self.settingView];
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:^{self.myView.frame = CGRectMake(0, 60, 320, 480);}
completion:^(BOOL finished) {}
];
} else if (self.myView){
self.myView.frame = CGRectMake(0, 60, 320, 480);
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{self.myView.frame = CGRectMake(0, 568, 320, 480);}
completion:^(BOOL finished) {}
];
[self.settingView removeFromSuperview];
self.settingView = nil;
[self.myView removeFromSuperview];
}
}
Thank you.
You dealloc the settings view before the animation starts. Try this:
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{self.myView.frame = CGRectMake(0, 568, 320, 480);}
completion:^(BOOL finished) {
[self.settingView removeFromSuperview];
self.settingView = nil;
[self.myView removeFromSuperview];
}
];
*Also: If you are using auto layout you need to do one of these options:
A. Set the constrains of myView instead of the frame, before the block, and then call only layoutIfNeeded: inside the block.
B. Set the transform property in the animation block.
Try adding this line
[self layoutIfNeeded];
as the last line within your animation block. Something like that:
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionTransitionFlipFromBottom
animations:^{
self.myView.frame = CGRectMake(0, 60, 320, 480);
// any other stuff you have to move
[self layoutIfNeeded];
}
completion:^(BOOL finished) {}
];

Animating a UIView to slide down, then slide up

Im trying to figure out why this isnt working
in my tableViewcontroller viewDidLoad
self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320,0)];
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 0)];
self.headerLabel.textAlignment = NSTextAlignmentCenter;
self.headerLabel.text = #"text";
[self.view addSubview:self.headerView];
[self.headerView addSubview:self.headerLabel];
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,15);
self.headerView.frame = CGRectMake(0, 5, 320,15);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,0);
self.headerView.frame = CGRectMake(0, 5, 320,0);
} completion:^(BOOL finished) {
}];
}];
if I remove the slide back up part in the completion block of the first animate call It works. The view slides down correctly. However I cannot get it
to shrink back up at all. When I include the slide up code in the completion block the view is not displayed at all on load and I dont know why and Im going insane
I'm not sure why the label disappears, but you can fix that by giving the view and label an appropriate height when you create them, and only animate the label's y position rather than its height.
- (void)viewDidLoad {
[super viewDidLoad];
self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -30, 320,30)];
self.headerView.backgroundColor = [UIColor yellowColor];
self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 21)];
self.headerLabel.textAlignment = NSTextAlignmentCenter;
self.headerLabel.text = #"text";
[self.view addSubview:self.headerView];
[self.headerView addSubview:self.headerLabel];
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerView.frame = CGRectMake(0, 0, 320,30);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerView.frame = CGRectMake(0, -30, 320,30);
} completion:^(BOOL finished) {
}];
}];
}
viewDidLoad is not really the best place to be starting animations. You should move the code to viewWillAppear: , and if you only want this to occur the first time the view appears, you should add a BOOL property to your controller (i.e. self.hasperformedInitialHeaderAnimation), so:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(!self.hasPerformedInitialHeaderAnimation){
self.hasPerformedInitalHeaderAnimation = YES;
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,15);
self.headerView.frame = CGRectMake(0, 5, 320,15);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.headerLabel.frame = CGRectMake(0, 5, 320,0);
self.headerView.frame = CGRectMake(0, 5, 320,0);
} completion:^(BOOL finished) {
}];
}

How to add subview with flip animation?

If you create a brand new single view app and put this code behind a button:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[self.view addSubview:blah];
}
completion:^(BOOL finished){
}];
The subview is added immediately with no animation. If you add the subview first then try to animate it... you get the same problem.
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:blah];
[UIView transitionWithView:blah duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
blah.backgroundColor = [UIColor grayColor];
}
completion:^(BOOL finished){
}];
How on Earth do you animate a flip for a subview while or immediately after adding it?
You generally need to have the container that constrains the animation to be in place already:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 100, 100);
_container = [[UIView alloc] initWithFrame:frame];
_container.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_container];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIView *subview = [[UIView alloc] initWithFrame:_container.bounds];
subview.backgroundColor = [UIColor darkGrayColor];
[UIView transitionWithView:_container
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[_container addSubview:subview];
}
completion:NULL];
}
This is worth a try:
UIView *blah = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
blah.backgroundColor = [UIColor grayColor];
[self.view addSubview:blah];
blah.alpha = 0.0; //Or with blah.hidden = TRUE and then FALSE inside the animation block
[UIView transitionWithView:blah
duration:1
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
blah.alpha = 1.0;
}
completion:^(BOOL finished){
}];

How to fit image in UITextView and Tap to get larger

I have created a Table-view in main ViewController once i touch the row it will take to the next view controller(DetailViewController) where it display the image and name of dish.And once i tap the image it should get larger but it is not getting bigger .The below code is which i have tried in DetailViewController.
DetailViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10,100, 100)];
CGRect textViewFrame = CGRectMake(10, 10, 300, 400);
textView = [[UITextView alloc] initWithFrame:textViewFrame];
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor=[UIColor whiteColor];
textView.editable=NO;
textView.delegate = self;
[self.view addSubview:textView];
[textView addSubview:imageView];
textView.alpha = 0.9;
textView.font = [UIFont systemFontOfSize:15];
if(mrowno==0)
{
imageView.image=[UIImage imageNamed:#"Dosa.jpg"];
textView.text = #"\n\n\n\n\n\n\n\nDOSA\nDosa, ";
imageButton = [[UIButton alloc]init];
[imageButton setFrame:CGRectMake(0, 0, 100, 100)];
[imageButton addTarget:self action:#selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:imageButton];
}
Here is the method to make image larger
-(void)imageTapped:(UIButton*)in_sender
{
[UIView transitionWithView:in_sender.superview duration:0.8f options:UIViewAnimationOptionCurveLinear animations:^
{
[in_sender.superview setFrame:CGRectMake(0, 0, 300, 500)];
}
completion:^(BOOL finished)
{
}];
}
You can increase and decrease the size of image by pinch in and pinch out,If it required
-(void)viewDidLoad
{
UIPinchGestureRecognizer* pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinching:)];
[self.imageView addGestureRecognizer:pinch];
}
-(void)pinching:(UIGestureRecognizer*) recognizer
{
UIPinchGestureRecognizer* pinch=(UIPinchGestureRecognizer*) recognizer;
self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
pinch.scale=1;
NSLog(#"pinching");
}
You do not want to use transitionWithView. Instead, you want to use + animateWithDuration: delay:options:animations:completion: (or any of its shorter variants...)
[UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
viewYouWantToAnimate.frame = CGRectMake(..., ..., ..., ...);
} completion:^{
//Add completion code here, or pass nil if you don't have anything to do.
}];
Edit: In case you were wondering what transitionWithView was used for... the AppleDocs have a great example of it adding animations for remove/addSubview methods (not frame modifications):
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[fromView removeFromSuperview];
[containerView addSubview:toView];
} completion:NULL];

Resources