View Full Version : Some Size issue I think.

Feb 27, 2012, 02:44 AM
Hi again..

I'm trying to animate with a UIImageView.
using this.

-(void)animateNewStampImage:(UIImage *)myStamp ToHere:(CGRect)finalRect

CGRect bounds = [self bounds];

CGPoint topLeftCorner;
CGPoint centre;
topLeftCorner.x = bounds.origin.x + bounds.size.width ; // in percentage of screen size //
topLeftCorner.y = bounds.origin.y + bounds.size.height; // in percentage of the screen size //

centre.x = bounds.origin.x + bounds.size.width / 2; // in percentage of screen size //
centre.y = bounds.origin.y + bounds.size.height/ 2; // in percentage of the screen size //
UIImage* animStampImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *myNewStamp = [[[UIImageView alloc]initWithImage:[myStamp stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0]]retain];

myNewStamp.bounds = CGRectMake(0,0,bounds.size.width,bounds.size.height);
//[myNewStamp setFrame:CGRectMake(0,0,bounds.size.width,bounds.size.height)];
//[myNewStamp setBounds:CGRectMake(0,0,bounds.size.width,bounds.size.height)];

//NSLog(@"animating Stamp");
//UIImageView * myNewStamp = [[UIImageView alloc] initWithImage:myStamp];
//[myNewStamp setFrame:CGRectMake(0,0,bounds.size.width,bounds.size.height)];

CABasicAnimation *opacityAnimm = [CABasicAnimation animationWithKeyPath:@"opacity"];
CABasicAnimation *scaleAnimm = [CABasicAnimation animationWithKeyPath:@"transform"];
CABasicAnimation *positionAimm = [CABasicAnimation animationWithKeyPath:@"position"];

scaleAnimm.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1,1,1.0)];
scaleAnimm.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(.1,.1,1.0)];
scaleAnimm.removedOnCompletion = YES;

CGPoint positionStampNew = finalRect.origin;
//positionAimm.fromValue =[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(positionStampNew.x,positionStampNew.y,0)];
//positionAimm.fromValue =[NSValue valueWithCATransform3D:CATransform3DMakeTranslation(positionStampNew.x,positionStampNew.y,0)];
positionAimm.fromValue =[NSValue valueWithCGPoint:positionStampNew];
//NSLog(@" x: %f y: %f",finalRect.origin.x,finalRect.origin.y);

CGPoint sumRandomPosition = CGPointMake(arc4random()%20+finalRect.origin.x,arc4random()%20+finalRect.origin.y);
positionAimm.toValue = [NSValue valueWithCGPoint:sumRandomPosition];
//positionAimm.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(sumRandomPosition.x,sumRandomPosition.y,0)];
positionAimm.removedOnCompletion = YES;
NSLog(@" x: %f y: %f to X:%f Y:%f",positionStampNew.x,positionStampNew.y,sumRandomPosition.x,sumRandomPosition.y);

opacityAnimm.toValue = [NSNumber numberWithFloat:0.6];
opacityAnimm.fromValue = [NSNumber numberWithFloat:0.1];
opacityAnimm.removedOnCompletion = YES;

CAAnimationGroup *animGroupp = [CAAnimationGroup animation];

animGroupp.animations = [NSArray arrayWithObjects:opacityAnimm,scaleAnimm,positionAimm,nil];
animGroupp.delegate = self;
animGroupp.duration = 1.4;

//[UIView setAnimationDidStopSelector:@selector(stopButton)];
// [myNewStamp layer] //

[[myNewStamp layer] addAnimation:animGroupp forKey:@"newStamAnim"];

[self addSubview:myNewStamp];
[myNewStamp release];


called from this inside:

- (void)drawRect:(CGRect)rect {
// stampPost is a CGPoint read from an array of objects
// stampSize is a CGSize read from an array of objects --- both are correct when NSLogged - printed

if (newStampMarker){
CGRect newStampRec = CGRectMake(stampPost.x,stampPost.y, stampSize.width, stampSize.height);
uiimage = [UIImage imageNamed:@"SmileyStampYellow.png"];
[self animateNewStampImage:uiimage ToHere:newStampRec];
// also using this to draw non new stamps works fine //
[uiimage drawInRect:CGRectMake(stampPost.x,stampPost.y, stampSize.width, stampSize.height)];

// and I add it to the View with this. //
UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();

UIImageView *stampOverlay = [[UIImageView alloc] initWithImage:blendedImage];

[blendedImage release];
//[uiimage release];

[stampOverlay setFrame:CGRectMake(0,0,bounds.size.width,bounds.size.height)];
[stampOverlay setAlpha:STAMPBASE];

[self addSubview:stampOverlay];


So my sticker appear ok but the animating ones are too big and not in the right spot, how do I scale them to match the view / drawInRect image.

P.S sorry for the messy code..

Feb 27, 2012, 03:59 PM
Ok think this is a Nube mistake.
but seem to work better on a Layer.

added this..

CALayer *newStampLayer = [[CALayer alloc] init];
[newStampLayer setBounds:bounds];

//newStampLayer //
CGImageRef refStamp = [myStamp CGImage];

[newStampLayer setContents:(id) refStamp];

[[self layer] addSublayer:newStampLayer];
[newStampLayer release];
// .... and later locked the sticker to final position //
animGroupp.removedOnCompletion = NO;// <----- this is so counter intuitive !!! //
animGroupp.fillMode = kCAFillModeForwards;
[newStampLayer addAnimation:animGroupp forKey:@"newStamAnim"];

Still looking for a way to relate one for one positioning on a scaled View

Feb 28, 2012, 01:03 AM
I kept picking scales to make the Sticker fit. but there has to be a better way..

Help !!!!

also tried to add rotation.. with ..

// I wasn't using the right animationWithKeyPath:
CABasicAnimation *rotateAnimm = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
// this is the right one for what I wanted
CGFloat rotRand = (arc4random()%42)*(M_PI/180); // radians //

rotateAnimm.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(rotRand,0,0,1)]; // tried 0,0,1 and 1,0,0 nothing..

// added to CAAnimationGroup of Course.. //

nothing spins.. :confused:

Ok got it.. see above .. stupidity...

Mar 12, 2012, 04:28 PM
Not sure if I'm barking up the wrong tree but is there a way to
Animate in Context? and would that solve my sizing problems??

but can't seem to find anything in setting it in the layer or animation