Some Size issue I think.

Discussion in 'iOS Programming' started by IDMah, Feb 27, 2012.

  1. IDMah, Feb 27, 2012
    Last edited: Feb 27, 2012

    macrumors regular

    Joined:
    May 13, 2011
    #1
    Hi again..

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

    Code:
    -(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();
    	UIGraphicsEndImageContext();
    	*/
    	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:
    Code:
    - (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();
    	UIGraphicsEndImageContext();
    	
    	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..
    thank
     
  2. thread starter macrumors regular

    Joined:
    May 13, 2011
    #2
    works better on Layer.

    Ok think this is a Nube mistake.
    but seem to work better on a Layer.

    added this..
    Code:
    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
     
  3. IDMah, Feb 27, 2012
    Last edited: Feb 27, 2012

    thread starter macrumors regular

    Joined:
    May 13, 2011
    #3
    fudged to work.

    I kept picking scales to make the Sticker fit. but there has to be a better way..

    Help !!!!

    also tried to add rotation.. with ..
    Code:
    // 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...
     
  4. thread starter macrumors regular

    Joined:
    May 13, 2011
    #4
    Animate in Context.

    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
    level.

    thanks
    Ian
     

Share This Page