CATransactions not working

  • Hi everyone,

    I've been struggling to work out how to animate some properties on my
    NSButton subclass. I have looked through all the examples given in
    Apple's docs, but can't work out why my code doesn't animate.

    My code is:
    [CATransaction begin]; // Begin grouping animated property changes

    if (highlightEffect == SGUIButtonOpaqueEffect) {

      [CATransaction begin];
      [CATransaction setValue:[NSNumber numberWithFloat:5.0f]
                            forKey:kCATransactionAnimationDuration];
      self.layer.opacity = 1.0;
      [CATransaction commit];

    } else if (highlightEffect == SGUIButtonSlideEffect) {

      [CATransaction begin];
      [CATransaction setValue:[NSNumber numberWithFloat:1.0f]
                            forKey:kCATransactionAnimationDuration];
      self.layer.bounds = CGRectMake(self.layer.position.x,
    self.layer.position.y, self.bounds.size.width + 10,
    self.bounds.size.height);
      [CATransaction commit];

      [CATransaction begin];
      [CATransaction setValue:[NSNumber numberWithFloat:2.0f]
                            forKey:kCATransactionAnimationDuration];
      self.layer.position = CGPointMake(self.layer.position.x -
    self.layer.bounds.size.width, self.layer.position.y);
      [CATransaction commit];

    }

    [CATransaction commit];

    Can anyone see any problems with this? Why aren't there any animations
    when this code executes?

    Any help greatly appreciated.

    Adam Radestock
    Glass Monkey Software
  • On May 18, 2008, at 12:00 , Adam Radestock wrote:

    > Hi everyone,
    >
    > I've been struggling to work out how to animate some properties on
    > my NSButton subclass. I have looked through all the examples given
    > in Apple's docs, but can't work out why my code doesn't animate.

    Where exactly are you invoking this code? Is it in awakeFromNib?

    /brian
  • It's in a function called from within the mouseDown: handler in my
    subclass. I just don't get why the implicit animation isn't working?

    On 18 May 2008, at 20:22, Brian Christensen wrote:

    > On May 18, 2008, at 12:00 , Adam Radestock wrote:
    >
    >> Hi everyone,
    >>
    >> I've been struggling to work out how to animate some properties on
    >> my NSButton subclass. I have looked through all the examples given
    >> in Apple's docs, but can't work out why my code doesn't animate.
    >
    > Where exactly are you invoking this code? Is it in awakeFromNib?
    >
    > /brian
    >
  • On May 19, 2008, at 1:06 , Adam Radestock wrote:

    > It's in a function called from within the mouseDown: handler in my
    > subclass. I just don't get why the implicit animation isn't working?

    A couple of things:

    1) Make sure that somewhere up the hierarchy of your button's
    superviews one of them has the "Wants Layer" option in IB checked, or
    that you're setting "[someSuperviewOfCustomButton setWantsLayer:YES]"
    in code somewhere.

    2) If #1 isn't the issue, it would help if you posted your entire
    mouseDown: method. I have run into an issue with animations not
    happening when they should be, but I don't know if your issue is the
    same as the one I had recently without seeing more context.

    /brian
  • This is actually on iPhone... Core Animation is always active on
    iPhone, there's no wantsLayer function...

    Here is all the relevent code:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; {
    [self doHighlightEffect:SGUIButtonOpaqueEffect];
    [super touchesBegan:touches withEvent:event];
    }

    //- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; {
    [self doHighlightEffect:SGUIButtonSlideEffect];
    [super touchesEnded:touches withEvent:event];
    }

    - (void)doHighlightEffect:(SGUIButtonHighlightEffect)highlightEffect; {
    [CATransaction begin]; // Begin grouping animated property changes

    if (highlightEffect == SGUIButtonOpaqueEffect) {

      [CATransaction begin];
      [CATransaction setValue:[NSNumber numberWithFloat:5.0f]
                            forKey:kCATransactionAnimationDuration];
      self.layer.opacity = 1.0;
      [CATransaction commit];

    } else if (highlightEffect == SGUIButtonSlideEffect) {

      [CATransaction begin];
      [CATransaction setValue:[NSNumber numberWithFloat:1.0f]
                            forKey:kCATransactionAnimationDuration];
      self.layer.bounds = CGRectMake(self.layer.position.x,
    self.layer.position.y, self.bounds.size.width + 10,
    self.bounds.size.height);
      [CATransaction commit];

      [CATransaction begin];
      [CATransaction setValue:[NSNumber numberWithFloat:2.0f]
                            forKey:kCATransactionAnimationDuration];
      self.layer.position = CGPointMake(self.layer.position.x -
    self.layer.bounds.size.width, self.layer.position.y);
      [CATransaction commit];

    }

    [CATransaction commit];
    }

    On 19 May 2008, at 18:23, Brian Christensen wrote:

    > On May 19, 2008, at 1:06 , Adam Radestock wrote:
    >
    >> It's in a function called from within the mouseDown: handler in my
    >> subclass. I just don't get why the implicit animation isn't working?
    >
    >
    > A couple of things:
    >
    > 1) Make sure that somewhere up the hierarchy of your button's
    > superviews one of them has the "Wants Layer" option in IB checked,
    > or that you're setting "[someSuperviewOfCustomButton
    > setWantsLayer:YES]" in code somewhere.
    >
    > 2) If #1 isn't the issue, it would help if you posted your entire
    > mouseDown: method. I have run into an issue with animations not
    > happening when they should be, but I don't know if your issue is the
    > same as the one I had recently without seeing more context.
    >
    > /brian
    >
  • Unfortunately, by mentioning the magic word ("iPhone"), you won't be getting
    any answers on this mailing list, since the iPhone SDK is still under an
    NDA...

    Cheers,

    Paul

    On Mon, May 19, 2008 at 7:03 PM, Adam Radestock <raddish...>
    wrote:

    > This is actually on iPhone... Core Animation is always active on iPhone,
    > there's no wantsLayer function...
    >
    > Here is all the relevent code:
    >
    > - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; {
    > [self doHighlightEffect:SGUIButtonOpaqueEffect];
    > [super touchesBegan:touches withEvent:event];
    > }
    >
    > //- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    >
    > - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; {
    > [self doHighlightEffect:SGUIButtonSlideEffect];
    > [super touchesEnded:touches withEvent:event];
    > }
    >
    > - (void)doHighlightEffect:(SGUIButtonHighlightEffect)highlightEffect; {
    > [CATransaction begin]; // Begin grouping animated property changes
    >
    > if (highlightEffect == SGUIButtonOpaqueEffect) {
    >
    > [CATransaction begin];
    > [CATransaction setValue:[NSNumber numberWithFloat:5.0f]
    >
    > forKey:kCATransactionAnimationDuration];
    > self.layer.opacity = 1.0;
    > [CATransaction commit];
    >
    > } else if (highlightEffect == SGUIButtonSlideEffect) {
    >
    > [CATransaction begin];
    > [CATransaction setValue:[NSNumber numberWithFloat:1.0f]
    >
    > forKey:kCATransactionAnimationDuration];
    > self.layer.bounds = CGRectMake(self.layer.position.x,
    > self.layer.position.y, self.bounds.size.width + 10,
    > self.bounds.size.height);
    > [CATransaction commit];
    >
    > [CATransaction begin];
    > [CATransaction setValue:[NSNumber numberWithFloat:2.0f]
    >
    > forKey:kCATransactionAnimationDuration];
    > self.layer.position = CGPointMake(self.layer.position.x -
    > self.layer.bounds.size.width, self.layer.position.y);
    > [CATransaction commit];
    >
    > }
    >
    > [CATransaction commit];
    >
    > }
    >
    > On 19 May 2008, at 18:23, Brian Christensen wrote:
    >
    > On May 19, 2008, at 1:06 , Adam Radestock wrote:
    >>
    >> It's in a function called from within the mouseDown: handler in my
    >>> subclass. I just don't get why the implicit animation isn't working?
    >>>
    >>
    >>
    >> A couple of things:
    >>
    >> 1) Make sure that somewhere up the hierarchy of your button's superviews
    >> one of them has the "Wants Layer" option in IB checked, or that you're
    >> setting "[someSuperviewOfCustomButton setWantsLayer:YES]" in code somewhere.
    >>
    >> 2) If #1 isn't the issue, it would help if you posted your entire
    >> mouseDown: method. I have run into an issue with animations not happening
    >> when they should be, but I don't know if your issue is the same as the one I
    >> had recently without seeing more context.
    >>
    >> /brian
    >>
    >>

    >
  • Exactly why I didn't say in the first place... :-)

    On 19 May 2008, at 19:09, Paul Bailey wrote:

    > Unfortunately, by mentioning the magic word ("iPhone"), you won't be
    > getting any answers on this mailing list, since the iPhone SDK is
    > still under an NDA...
    >
    > Cheers,
    >
    > Paul
    >
    > On Mon, May 19, 2008 at 7:03 PM, Adam Radestock <raddish...>
    >> wrote:
    > This is actually on iPhone... Core Animation is always active on
    > iPhone, there's no wantsLayer function...
    >
    > Here is all the relevent code:
    >
    > - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; {
    > [self doHighlightEffect:SGUIButtonOpaqueEffect];
    > [super touchesBegan:touches withEvent:event];
    > }
    >
    > //- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    >
    > - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; {
    > [self doHighlightEffect:SGUIButtonSlideEffect];
    > [super touchesEnded:touches withEvent:event];
    > }
    >
    > - (void)doHighlightEffect:
    > (SGUIButtonHighlightEffect)highlightEffect; {
    > [CATransaction begin]; // Begin grouping animated property
    > changes
    >
    > if (highlightEffect == SGUIButtonOpaqueEffect) {
    >
    > [CATransaction begin];
    > [CATransaction setValue:[NSNumber numberWithFloat:5.0f]
    >
    > forKey:kCATransactionAnimationDuration];
    > self.layer.opacity = 1.0;
    > [CATransaction commit];
    >
    > } else if (highlightEffect == SGUIButtonSlideEffect) {
    >
    > [CATransaction begin];
    > [CATransaction setValue:[NSNumber numberWithFloat:1.0f]
    >
    > forKey:kCATransactionAnimationDuration];
    > self.layer.bounds = CGRectMake(self.layer.position.x,
    > self.layer.position.y, self.bounds.size.width + 10,
    > self.bounds.size.height);
    > [CATransaction commit];
    >
    > [CATransaction begin];
    > [CATransaction setValue:[NSNumber numberWithFloat:2.0f]
    >
    > forKey:kCATransactionAnimationDuration];
    > self.layer.position =
    > CGPointMake(self.layer.position.x - self.layer.bounds.size.width,
    > self.layer.position.y);
    > [CATransaction commit];
    >
    > }
    >
    > [CATransaction commit];
    >
    > }
    >
    > On 19 May 2008, at 18:23, Brian Christensen wrote:
    >
    > On May 19, 2008, at 1:06 , Adam Radestock wrote:
    >
    > It's in a function called from within the mouseDown: handler in my
    > subclass. I just don't get why the implicit animation isn't working?
    >
    >
    > A couple of things:
    >
    > 1) Make sure that somewhere up the hierarchy of your button's
    > superviews one of them has the "Wants Layer" option in IB checked,
    > or that you're setting "[someSuperviewOfCustomButton
    > setWantsLayer:YES]" in code somewhere.
    >
    > 2) If #1 isn't the issue, it would help if you posted your entire
    > mouseDown: method. I have run into an issue with animations not
    > happening when they should be, but I don't know if your issue is the
    > same as the one I had recently without seeing more context.
    >
    > /brian
    >
  • Once again.

    Discussion of the iPhone SDK is NOT allowed here. It is under NDA.

    Thanks

    Scott
    moderator

    On May 19, 2008, at 2:03 PM, Adam Radestock wrote:

    > This is actually on iPhone... Core Animation is always active on
    > iPhone, there's no wantsLayer function...
previous month may 2008 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Go to today