Jump to content
13 posts in this topic

Recommended Posts

Here is my code, if I add the CABasicAnimation,

 

the UITouch *touch = [[event allTouches] anyObject]; will return to MainView not UIImageView,

 

is there other way to add the CABasicAnimation?

 

MainView.m

 

- (void)createGame:(int)numberDot
{
for(int i = 0; i <numberDot; i++) {
	float xPos = 0;
	float yPos = 60 * i;
	imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, yPos, 57.0, 57.0)];
	imageView.image =[UIImage imageNamed:@"Icon8.png"];
	imageView.userInteractionEnabled = YES;
	[self addSubview:imageView];
	[imageViewArray insertObject:imageView atIndex:i]; 

	CABasicAnimation *theAnimation;
	theAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
	theAnimation.duration = 20.0;
	theAnimation.repeatCount = 2;
	theAnimation.autoreverses = YES;
	theAnimation.fromValue = [NSNumber numberWithFloat:10.0];
	theAnimation.toValue = [NSNumber numberWithFloat:300.0];
	[[imageView layer] addAnimation:theAnimation forKey:@"animateOpacity"];

	[imageView release];
}
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{	
UITouch *touch = [[event allTouches] anyObject];
for(int i = 0; i < [imageViewArray count]; i++) 
{
	id anObject = [[imageViewArray objectAtIndex:i] retain];

	if([touch view] == anObject)
	{
		[anObject removeFromSuperview];
		[imageViewArray removeObjectAtIndex:i];
	}
}
}

Link to comment
https://www.insanelymac.com/forum/topic/119813-cabasicanimation-problem/
Share on other sites

First of all, you shouldn't release imageview at the end of the first function.

 

You also shouldn't retain it when taking it from the array. Retaining takes away the pointer that was there, and makes a new copy of it, so there is no more pointer. You want a pointer and you want it to be the same one that you added, so you can remove it from superview.

 

This may be your problem

 

Hope it helps!

As long as you have the "imageView.userInteractionEnabled = YES;" method, you don't

 

Yes, you do. He's trying to customize what happens on a touch event. That requires subclassing the method.

 

Edit: well, you're right, you don't need to subclass it. But it's probably the better way to do it.

lamfan: use -setFrame: and pass a CGRectMake() as the argument.

 

yes I use the setFrame method of UIView, the image did move but the touch will not return the UIimageView,

 

it return the mainView again......

 

what's wrong with that?

 

 

- (void)createGame:(int)numberDot
{
for(int i = 0; i < numberDot; i++) {
	float xPos = 10;
	float yPos = 60 * i + 10;
	imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, yPos, 57.0, 57.0)];
	imageView.image =[UIImage imageNamed:@"Icon8.png"];
	imageView.userInteractionEnabled = YES;

	[self addSubview:imageView];
	[imageViewArray insertObject:imageView atIndex:i]; 

	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:5.0];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
	CGRect tempFrame = [imageView frame];
	tempFrame.origin.x = 120.0f;
	[imageView setFrame:tempFrame];
	[UIView commitAnimations];
}
}

Finally I find out a stupid way to make it work!

 

I'm using NSTimer to update the frame position of UIView,

 

so the touchesBegan event will respond.

 

That also mean I can't apply the UIView Animation and CAAinimation to my UIView........

 

so sad.......... :)

×
×
  • Create New...