Zoom UIView by using CGAffineTransformMakeScale and white background - uiview

I try to scale an UIView. And there is a white background outside of an UIview. How I can take it out to see just background view? Thanks!
self.view.transform = CGAffineTransformMakeScale(.5,.5);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.9];
self.view.transform = CGAffineTransformMakeScale(1,1);
[UIView commitAnimations];

Try setting the background color of the view you need to be transparent to [UIColor clearColor]:
myView.backgroundColor = [UIColor clearColor];

Related

Animated change colour of UIView

I want to change colour of my view while swiping but i don't know how to do animated colour change. I'll be grateful for the help.
backgroundColor is animatable right out of the box 👍🏻
[UIView animateWithDuration:0.5 delay:1.0 options: UIViewAnimationCurveEaseOut animations:^{
//this is the animation block here
myView.backgroundColor = newColor;
} completion:^(BOOL finished) {
if (finished){
NSLog(#"Colour animation complete!");
}
}];
Work with CALayer, if you search for it you could find lot's of easy examples, this one for example, from Ray Wenderlich, which i find great.
UIView
- (void)changeColorWithFillingAnimation {
//we create a view that will increase in size from top to bottom, thats why it starts with these frame parameters
UIView *fillingView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 1, self.view.bounds.size.width, 1)];
//set the color we want to change to
fillingView.backgroundColor = [UIColor blueColor];
//add it to the view we want to change the color
[self.view addSubview:fillingView];
[UIView animateWithDuration:2.0 animations:^{
//this will make so the view animates up, since its animating the frame to the target view's size
fillingView.frame = self.view.bounds;
} completion:^(BOOL finished) {
//set the color we want and then disappear with the filling view.
self.view.backgroundColor = [UIColor blueColor];
[fillingView removeFromSuperview];
}];
}
CAShapeLayer + CABasicAnimation + CATransition
- (void)changeColorWithShapeLayer {
//create the initial and the final path.
UIBezierPath *fromPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, self.view.bounds.size.height - 1, self.view.bounds.size.width, 1)];
UIBezierPath *toPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
//create the shape layer that will be animated
CAShapeLayer *fillingShape = [CAShapeLayer layer];
fillingShape.path = fromPath.CGPath;
fillingShape.fillColor = [UIColor blueColor].CGColor;
//create the animation for the shape layer
CABasicAnimation *animatedFill = [CABasicAnimation animationWithKeyPath:#"path"];
animatedFill.duration = 2.0f;
animatedFill.fromValue = (id)fromPath.CGPath;
animatedFill.toValue = (id)toPath.CGPath;
//using CATransaction like this we can set a completion block
[CATransaction begin];
[CATransaction setCompletionBlock:^{
//when the animation ends, we want to set the proper color itself!
self.view.backgroundColor = [UIColor blueColor];
}];
//add the animation to the shape layer, then add the shape layer as a sublayer on the view changing color
[fillingShape addAnimation:animatedFill forKey:#"path"];
[self.view.layer addSublayer:fillingShape];
[CATransaction commit];
}
One very simple way to accomplish this would be to use a UIView animation block.
[UIView animateWithDuration:1.0 animations:^{
view.backgroundColor = [UIColor redColor];
}];
Assumming that you have implemented left swipe gesture,
UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];
// Implement Gesture Methods
-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do what you want here
[UIView animateWithDuration:.5
animations:^{
self.view.backgroundColor = [UIColor yellowColor];
}
completion:^(BOOL finished){
NSLog(#"completion block");
}];
}
}

Unable to add subviews to a UIView when using the View's layer as a mask

I am trying to animate a UIView with a popover kind of effect, and I want the background of the UIView to have that ios7 style blur as the background.
Making this slightly more complicated, I'd also like to be able to apply a transform to the popover and its contents by dragging the corner, much like a chat in the Facebook app.
Now, my approach is basically this: take a blurred snapshot of the window, add a UIImageView as a subview of the controller's view, mask that imageView with the UIView popover. However, I am then finding that I can not interact with that UIView at all - subviews don't show up, gesture recognizers don't fire, etc.
Here is my code:
UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
CGRect frame = [self.tableView.tableHeaderView convertRect:self.displayView.frame toView:self.view];
UIView *view = [UIView new];
view.frame = frame;
view.backgroundColor = [UIColor blackColor];
view.layer.cornerRadius = 10.f;
view.tag = 100;
//Take the snapshot
UIGraphicsBeginImageContextWithOptions(mainWindow.rootViewController.view.bounds.size, YES, [[UIScreen mainScreen] scale]);
[mainWindow drawViewHierarchyInRect:mainWindow.bounds afterScreenUpdates:NO];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *blur = [screenImage applyDarkEffect];
UIImageView *imageView = [[UIImageView alloc] initWithImage:blur];
imageView.frame = self.view.bounds;
[self.view addSubview:imageView];
[self.view addSubview:view];
imageView.layer.mask = view.layer;
//Animate the view
[UIView animateKeyframesWithDuration:1.4f delay:0.0f options:UIViewKeyframeAnimationOptionCalculationModeCubic
animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.75 animations:^{
view.frame = CGRectInset(mainWindow.frame, 30, 40);
}];
[UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.05 animations:^{
view.frame = CGRectInset(mainWindow.frame, 20, 30);
}];
[UIView addKeyframeWithRelativeStartTime:0.8 relativeDuration:0.20 animations:^{
view.frame = CGRectInset(mainWindow.frame, 25, 35);
}];
} completion:^(BOOL finished) {
//This does nothing.
UIView *test = [UIView new];
test.frame = CGRectMake(100, 100, 100, 100);
test.backgroundColor = [UIColor redColor];
[view addSubview:test];
}];
Any insights in to why this isn't working? Or alternative approaches?
One alternate approach I thought of would be to roll my own keyframe animation: Add the imageView as a subview of the 'view', use an update timer, and calculate the frame of the view for each frame, offsetting the image view appropriately. But I was hoping there would be an easier way around this issue.

Animation of alpha on UIView not working

I'm making a custom segue that fades to black, swaps out the view controller and then fades back to transparent. Unfortunately, I can't seem to get it to do that. Here's the code:
- (void)perform {
UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [self.sourceViewController navigationController].view.frame.size.width, [self.sourceViewController navigationController].view.frame.size.height)];
[overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.0]];
[[self.sourceViewController navigationController].view addSubview:overlay];
[UIView animateWithDuration:2.0 delay:0.0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
[overlay setAlpha:1.0];
}completion:^(BOOL finished) {
[[self.sourceViewController navigationController] pushViewController:[self destinationViewController] animated:NO];
[UIView animateWithDuration:2.0 delay:0.0 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
[overlay setAlpha:0.0];
}completion:^(BOOL finished) {
[overlay removeFromSuperview];
NSLog(#"Done!");
}];
}];
}
So, what I've done is add a UIView to the navigation controller that has a black background and an alpha of 0. Then, I do an animation to change the alpha to 1, push the destination view controller onto the navigation controller, and then animate back to clear. When I do this, nothing happens. It just drops the destination view controller onto the navigation controller without any kind of animation.
Oddly, if I start with an alpha of 1 and animate to 0 and then back to 1, it works (but it isn't what I want).
I'm thinking that there's some optimization happening that excludes my clear UIView and so changing the transparency is meaningless.
What happens if you do this?
...
[overlay setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]];
[overlay setAlpha:0.0];
[[self.sourceViewController navigationController].view addSubview:overlay];
...

UIView block animation move view left to right and back to left

I wan't to animate a view that's shaped as an arrow, and I want it to animate from left to right, left to right and so on..
This code below is not working, I'm guessing I need a path, but don't know how to do that in a UIView block animation.
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionRepeat animations:^{
controller.view.frame = frame1;
controller.view.frame = frame2;
} completion:^(BOOL finished) {
}];
You can use UIViewAnimationOptionAutoreverse option, try the code below:
UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(20.0f, 100.0f, 300.0f, 200.0f)];
[testView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:testView];
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^{
[testView setFrame:CGRectMake(0.0f, 100.0f, 300.0f, 200.0f)];
}
completion:nil];
[testView release];
It'll repeat move from right to left, left to right, ... smoothly.
Assuming that frame1 is the starting position and frame2 is the end position before repeat.
The problem is that the animation is calculated at the end of the runloop therefore the frame1 is ignored and the view is simply moved to frame2. If you move the setting of frame1 outside of the block then it should work fine. If you want the animation to play back and forward you will need to make it autoreverse with UIViewAnimationOptionAutoreverse

Switch XIB's fading through Black?

I am attempting to switch views (XIB's) while fading through black. I have been doing this using UIView animations and I want to keep it that way. The problem is that whenever I switch views I am not fading through black, its more of a fade directly to the next XIB.
How would I properly do this?
Here is my code (self.view = view1.view in my case, but maybe I shouldn't do that):
[self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]];
[UIView animateWithDuration:0.4
animations:^{
[view1.view setAlpha:0];
}
completion:^(BOOL finished){
[self.view.superview addSubview:view2.view];
[view2.view setAlpha:0];
[view2.view setFrame:CGRectMake(0, 0, 320, 480)];
[UIView animateWithDuration:0.4
animations:^{
[view2.view setAlpha:1];
}
completion:^(BOOL finished){
[view1.view removeFromSuperview];
}];
Thanks!
I think your problem is that the background color of self.view is part of that view. When you fade the view, it fades the background too.
Instead, you should put a view that you keep on the screen all the time, that's empty and has a black background color, and is behind view1 or view2.

Resources