How to update UIView background smoothly - objective-c

in my app I want to change the background of an uiview based on camera, All works properly but the problem is the background is changed too sharply (I think because of the focus).
So there's a way to change the background color of a UIView smoothly?
I actually use this code:
dispatch_async(dispatch_get_main_queue(), ^ {
[myview setBackgroundColor:[UIColor colorWithRed:red green:green blue:blue alpha:1]];
});
The UIVIew animation solution doesn't work beacause the changes are too fast.

Just before setting the background colour do something like;
CATransition *transitionAnimation = [CATransition animation];
[transitionAnimation setType:kCATransitionFade];
[transitionAnimation setDuration:0.2f];
[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[transitionAnimation setFillMode:kCAFillModeBoth];
[myview layer addAnimation:transitionAnimation forKey:#"fadeAnimation"];
Hopefully this will give you a gentle fade in colour change.

Related

Change UITableViewCell background color without animation

Changing of cell background color have some weird animation. I want to change this color immediately without animation. Maybe I should override setBackgroundColor method in subclass somehow?
cell.backgroundColor = [UIColor redColor];
Probably that you are changing that color while a UIView animation is in progress (a transition, a tableView row update, a UIView animation block, etc).
Try the following:
[UIView performWithoutAnimation:^{
cell.backgroundColor = [UIColor redColor];
}];
UPDATE: I'm not sure what kind of animation you got though. I made a simple test UITableViewController that change the cell color without animation when you tap on them (using what you specified in your comment), and it works.

How do I animate the movement of a view in iOS?

I'm a very beginner programmer and I'm trying to find the easiest way to do this. I have never done anything with animation before. I want to try to animate an image in my app, and I've run into some problems. This is the code someone previously suggested I use (from a different question):
imageView.image = yourLastImage;
// Do this first so that after the animation is complete the image view till show your last image.
NSArray * imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
nil];
// Note: here you may instead want to use something like [UIImage imageWithContentsOfFile:[self localImagePath:NO]] instead depending upon your targeted iOS version.
UIImageView * animatedImageView = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 125, 150, 130)];
animatedImageView.animationImages = imageArray;
animatedImageView.animationDuration = 1.1;
myAnimation.animationRepeatCount = 1;
animatedImageView.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:animatedImageView];
[animatedImageView startAnimating];
Well, first off, is this even practical? I want to animate something that goes from the middle of the screen to the bottom of the screen. Does that mean I have to have 500 images, each 1 pixel away from each other? What is the optimal pixel interval, so that it looks fluid and even and doesn't require a ton of work? Is there a better way to animate than the way above? Is there a program that helps make animations that you can later add into Xcode?
Also, can someone please explain the code above? I'm new to animation, I don't really understand what all that means.
Sorry if this is a stupid question, I said at the opening that I am a beginner. Thanks for any help you may provide!
For most animations, you can simply change the properties of the view inside a UIView animation block. The UIView class documentation lists which properties are animatable.
[UIView animateWithDuration:0.5f animations:^{
aView.frame = CGRectOffset(aView.frame, 0, 250);
}];
Here is a sample project demonstrating how you would trigger an animation when a button is pressed. You could put this animation code many other places, so there isn't a good answer to your question about where to put the code unless you give more details about what you want.
https://github.com/MaxGabriel/AnimationDemonstration
The approach you were taking was animating a UIImageView. I've never done this, but my understanding is that's like making an animated GIF. For things like moving views or fading them out, you'll want to use the animation block method shown above.
It is simple to do animation in iOS.
CGRect basketTopFrame = self.upperview.frame;
basketTopFrame.origin.y = -basketTopFrame.size.height;
CGRect basketBottomFrame = self.lowerview.frame;
basketBottomFrame.origin.y = self.view.bounds.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.upperview.frame = basketTopFrame;
self.lowerview.frame = basketBottomFrame;
[UIView commitAnimations];
The following code work for me, to move a view when open a keyboard or other things.
[UIView animateWithDuration:(timeYouWantAnimate - 0.3f) animations:^{
[nameOfView setFrame:CGRectMake(0,spaceYouWantMove like -100, self.view.with, self.view.height)];
}
Hope can help you too :D

CALayer and drawRect

I'm completely new to Core Animation, CALayer and all this stuff, so bear with me.
I have a custom NSTextField using as a Label. I would want the content to animate it's position, so the whole string get's visible if it's too long for the Labels width.
Now, the animation itself is working fine. I've implemented this with CABasicAnimation:
- (void)awakeFromNib {
CALayer *newLayer = [CALayer layer];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"position"];
[animation setFromValue:[NSValue valueWithPoint:NSMakePoint(0, 0)]];
[animation setToValue:[NSValue valueWithPoint:NSMakePoint(-self.attributedStringValue.size.width, 0)]];
[animation setDuration:5.0];
[animation setRepeatCount:HUGE_VAL];
[newLayer addAnimation:animation forKey:#"position"];
[self setLayer:newLayer];
[self setWantsLayer:YES];
}
The only problem is, that the drawRect: method only draws what's on the screen.
So I thought I would override the drawRect: method to draw the whole attributed string. But if I do this, nothing get's drawn at all...
Can anyone point me into the right direction?
Thank you!
In general, you want to avoid overriding drawRect if at all possible, especially for CALayer objects that you are animating. That tends to lead to really dreadful performance.
What do you mean "the drawRect: method only draws what's on the screen?"
It only draws the portion of the string that's currently visible?
I ended up using an NSTimer. Not the most beautiful solution, but at least it works.

Custom NSButton with semi-transparent background

I'm trying to create a custom NSButton with a 50% opaque black background and white text. To do this I've subclassed NSButton and overloaded DrawRect:
- (void) drawRect:(NSRect)dirtyRect
{
[self setBordered:NO];
//REMED since it has same effect as NSRectFill below
//[[self cell] setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.2]];
NSColor* backgroundColor = [NSColor colorWithCalibratedWhite:0 alpha:0.3f];
[backgroundColor setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
The white text appears fine but the button's background is always 100% opaque. The alpha value is not interpreted.
Any ideas? Thanks!
The default operation of NSRectFill() is copy which is not what you want. Replace it with
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceAtop);
Another solution I found was to keep my code the same but turn on the Core Animation Layer for each button in Interface Builder. I don't know enough about Core Animation Layer to know why this worked. I had previously turned CAL off because it was making my fonts look very jagged.

Animating the UIButton Layer?

I'm trying to bring the ripple effect seen in the dashboard application to iphone. My idea is whenever i place a button in the layout view there should be ripple effect around the button.
However, I`m able to bring the ripple effect for the whole view but I needs the effect only around the button. I don know where I went wrong. I tried the following code.
LayoutButton *tempLayoutButton=[[LayoutButton alloc] initWithObject:object];
tempLayoutButton.center=copyImage.center;
[layoutView addSubview:tempLayoutButton];
CALayer *templayer=[CALayer layer];
tempLayoutButton.layer.masksToBounds=NO;
templayer.frame=CGRectMake(0,0,50,50);
[tempLayoutButton.layer addSublayer:templayer];
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 1.0f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = #"rippleEffect";
[templayer addAnimation:animation forKey:#"animation"];
[tempLayoutButton release];
[tempLayoutButton.layer addSublayer:templayer];
If I replace templayer with LayoutView layer I can see the riple effect from the whole view. Can anyone tell me the solution
Thanks in advance
I found the answer in one of apples mailing list. The animation is apple`s own animation and no one can use that. And there is no guarantee for the animation to appear properly in the screen. If we need the same type of animation we need to manipulate the layers and get the animation