Core Animation is not working with “alpha” value
Before this code, my movie pic alpha is set to 0,
CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"alpha"];
Nothing happened, if I set alpha to 0.5 beforehand instead, the alpha remains at 0.5 and not animating to 1.
I've seen a code using UIView beginAnimations:
around, but I'm teaching core animation so I wondered why CABasicAnimation can't do simple task like this?
ios core-animation alpha cabasicanimation
add a comment |
Before this code, my movie pic alpha is set to 0,
CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"alpha"];
Nothing happened, if I set alpha to 0.5 beforehand instead, the alpha remains at 0.5 and not animating to 1.
I've seen a code using UIView beginAnimations:
around, but I'm teaching core animation so I wondered why CABasicAnimation can't do simple task like this?
ios core-animation alpha cabasicanimation
add a comment |
Before this code, my movie pic alpha is set to 0,
CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"alpha"];
Nothing happened, if I set alpha to 0.5 beforehand instead, the alpha remains at 0.5 and not animating to 1.
I've seen a code using UIView beginAnimations:
around, but I'm teaching core animation so I wondered why CABasicAnimation can't do simple task like this?
ios core-animation alpha cabasicanimation
Before this code, my movie pic alpha is set to 0,
CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"alpha"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"alpha"];
Nothing happened, if I set alpha to 0.5 beforehand instead, the alpha remains at 0.5 and not animating to 1.
I've seen a code using UIView beginAnimations:
around, but I'm teaching core animation so I wondered why CABasicAnimation can't do simple task like this?
ios core-animation alpha cabasicanimation
ios core-animation alpha cabasicanimation
edited Apr 14 '13 at 4:43
asked Sep 23 '11 at 1:49
5argon
1,24811531
1,24811531
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
[CABasicAnimation animationWithKeyPath:@"opacity"];
UIView exposes this as alpha
where as CALayer exposes this as opacity
.
What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
– 5argon
Sep 23 '11 at 16:30
4
@Sargon With Core Animation you are animatingCALayer
properties, notUIView
properties, so the layer is the place to look for correct property names - see:view.layer
.
– Palimondo
Sep 27 '11 at 12:23
add a comment |
@ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation
please refer to Apple's documentation
add a comment |
For Swift:
let opacity = CABasicAnimation(keyPath: "opacity")
opacity.fromValue = fromValue
opacity.toValue = toValue
opacity.duration = duration
opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed
view.layer.add(opacity, forKey: nil)
If you want to keep the final alpha
value, you have to set the current view controller as the delegate of the opacity animation:
opacity.delegate = self
And, in the delegate function animationDidStop
, you should do:
extension ViewController: CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
view.alpha = toValue
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7523441%2fcore-animation-is-not-working-with-alpha-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
[CABasicAnimation animationWithKeyPath:@"opacity"];
UIView exposes this as alpha
where as CALayer exposes this as opacity
.
What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
– 5argon
Sep 23 '11 at 16:30
4
@Sargon With Core Animation you are animatingCALayer
properties, notUIView
properties, so the layer is the place to look for correct property names - see:view.layer
.
– Palimondo
Sep 27 '11 at 12:23
add a comment |
[CABasicAnimation animationWithKeyPath:@"opacity"];
UIView exposes this as alpha
where as CALayer exposes this as opacity
.
What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
– 5argon
Sep 23 '11 at 16:30
4
@Sargon With Core Animation you are animatingCALayer
properties, notUIView
properties, so the layer is the place to look for correct property names - see:view.layer
.
– Palimondo
Sep 27 '11 at 12:23
add a comment |
[CABasicAnimation animationWithKeyPath:@"opacity"];
UIView exposes this as alpha
where as CALayer exposes this as opacity
.
[CABasicAnimation animationWithKeyPath:@"opacity"];
UIView exposes this as alpha
where as CALayer exposes this as opacity
.
edited Sep 23 '11 at 4:24
Evan
4,96111843
4,96111843
answered Sep 23 '11 at 3:12
ohho
29.7k59214347
29.7k59214347
What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
– 5argon
Sep 23 '11 at 16:30
4
@Sargon With Core Animation you are animatingCALayer
properties, notUIView
properties, so the layer is the place to look for correct property names - see:view.layer
.
– Palimondo
Sep 27 '11 at 12:23
add a comment |
What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
– 5argon
Sep 23 '11 at 16:30
4
@Sargon With Core Animation you are animatingCALayer
properties, notUIView
properties, so the layer is the place to look for correct property names - see:view.layer
.
– Palimondo
Sep 27 '11 at 12:23
What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
– 5argon
Sep 23 '11 at 16:30
What O_o I'll try this soon. But is there anymore case like this? And where can I check that what can I use for KeyPath? (normally I'd check on object's property which has setAlpha in it but in this case it doesn't match...)
– 5argon
Sep 23 '11 at 16:30
4
4
@Sargon With Core Animation you are animating
CALayer
properties, not UIView
properties, so the layer is the place to look for correct property names - see: view.layer
.– Palimondo
Sep 27 '11 at 12:23
@Sargon With Core Animation you are animating
CALayer
properties, not UIView
properties, so the layer is the place to look for correct property names - see: view.layer
.– Palimondo
Sep 27 '11 at 12:23
add a comment |
@ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation
please refer to Apple's documentation
add a comment |
@ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation
please refer to Apple's documentation
add a comment |
@ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation
please refer to Apple's documentation
@ohho answers the posted question. Mine will be a bit more generic. For a list what can and how be animated with CABasicAnimation
please refer to Apple's documentation
answered Oct 28 '15 at 10:57
Julian Król
7,82043759
7,82043759
add a comment |
add a comment |
For Swift:
let opacity = CABasicAnimation(keyPath: "opacity")
opacity.fromValue = fromValue
opacity.toValue = toValue
opacity.duration = duration
opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed
view.layer.add(opacity, forKey: nil)
If you want to keep the final alpha
value, you have to set the current view controller as the delegate of the opacity animation:
opacity.delegate = self
And, in the delegate function animationDidStop
, you should do:
extension ViewController: CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
view.alpha = toValue
}
}
add a comment |
For Swift:
let opacity = CABasicAnimation(keyPath: "opacity")
opacity.fromValue = fromValue
opacity.toValue = toValue
opacity.duration = duration
opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed
view.layer.add(opacity, forKey: nil)
If you want to keep the final alpha
value, you have to set the current view controller as the delegate of the opacity animation:
opacity.delegate = self
And, in the delegate function animationDidStop
, you should do:
extension ViewController: CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
view.alpha = toValue
}
}
add a comment |
For Swift:
let opacity = CABasicAnimation(keyPath: "opacity")
opacity.fromValue = fromValue
opacity.toValue = toValue
opacity.duration = duration
opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed
view.layer.add(opacity, forKey: nil)
If you want to keep the final alpha
value, you have to set the current view controller as the delegate of the opacity animation:
opacity.delegate = self
And, in the delegate function animationDidStop
, you should do:
extension ViewController: CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
view.alpha = toValue
}
}
For Swift:
let opacity = CABasicAnimation(keyPath: "opacity")
opacity.fromValue = fromValue
opacity.toValue = toValue
opacity.duration = duration
opacity.beginTime = CACurrentMediaTime() + beginTime //If a delay is needed
view.layer.add(opacity, forKey: nil)
If you want to keep the final alpha
value, you have to set the current view controller as the delegate of the opacity animation:
opacity.delegate = self
And, in the delegate function animationDidStop
, you should do:
extension ViewController: CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
view.alpha = toValue
}
}
edited Nov 20 '18 at 8:09
answered Nov 19 '18 at 12:05
Ginés SM
633
633
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7523441%2fcore-animation-is-not-working-with-alpha-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown