Link to home
Start Free TrialLog in
Avatar of jledezma
jledezma

asked on

Trigger animation in ClassA from a thouched method in ClassB

I have an UIViewController(ViewController Class) with an imageView that loads and animation(startAnimation method) which is triggered when a UIButton is clicked. The button was inserted only for testing purposes. Then I have a UIView (TocaArea Class) that register touches on a context and depending on coordinates of the movement should trigger the animation on ViewController's startAnimation method.

I'm trying:

ViewController *anim = [[ViewController alloc]init];
        [anim startAnimation];

Open in new window


but even though I can build and run without errors it just doesn't triggers the animation. The UIButton triggers the animation and the gestures are being traced as it should.

Here are the files involved...

ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {    
    IBOutlet UIImageView *animation;
}
- (IBAction)startAnimation; // *** this triggers the method perfectly ***
@end

Open in new window


ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)startAnimation {

    [animation setAnimationRepeatCount:1];
    animation.animationDuration = 3;
    [animation startAnimating];
}

- (void)viewDidLoad
{
    int n, numeral;
    NSMutableArray *imagenesArray = [[NSMutableArray alloc] init];
    for (n=2; n<91; n++) {
        numeral=n+10000;
        NSMutableString* imagenNombre= [NSMutableString stringWithFormat:@"Overweight 0%d.png", numeral];
        UIImage *imagen = [UIImage imageNamed:imagenNombre];
        [imagenesArray addObject: imagen];
    }
    animation.animationImages=imagenesArray;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{

    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

@end

Open in new window


TocaArea.h
#import <UIKit/UIKit.h>

@interface TocaArea : UIView {

    NSMutableArray *pointArray;

}

@property (nonatomic, retain) NSMutableArray *pointArray;

@end


**TocaArea.m**
#import "TocaArea.h"
#import "PointLocation.h"
#import "ViewController.h"

@implementation TocaArea

@synthesize pointArray;

float xi,xf,yi,yf;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [[UIColor greenColor] setStroke];
    CGContextSetLineWidth(ctx, 5);
    CGContextSetLineCap(ctx, kCGLineCapRound);

    PointLocation *tempPoint;

    CGContextBeginPath(ctx);

    for (int i=0; i<[pointArray count]; i++) {
        tempPoint = [pointArray objectAtIndex:i];
        if (i==0) {
            CGContextMoveToPoint(ctx, tempPoint.location.x, tempPoint.location.y);
            continue;
        } else {
            //CGContextAddLineToPoint(ctx, tempPoint.location.x, tempPoint.location.y);
            // ^ ^ why is this returning a "no current point" run time error? ^ ^
        }

        CGContextDrawPath(ctx, kCGPathStroke);

        CGContextSetLineWidth(ctx, 1);
        CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 0.5);
        CGContextAddArc(ctx, tempPoint.location.x, tempPoint.location.y, 20, 0, M_PI*2, YES);
        CGContextStrokePath(ctx);

    }

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    NSArray *allTouches = [[event allTouches] allObjects];

    if ([allTouches count] > 1)
        return;
    else {

        self.pointArray = [[NSMutableArray alloc] init];
        UITouch *touch =[allTouches objectAtIndex:0];
        PointLocation *currentLoc = [[PointLocation alloc] init];
        currentLoc.location = [touch locationInView:self];

        xi = currentLoc.location.x;
        yi = currentLoc.location.y;

        [pointArray addObject:currentLoc];

        [self setNeedsDisplay];
    }
    NSLog(@"starts touch");

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSArray *allTouches = [[event allTouches] allObjects];

    if ([allTouches count] > 1)
        return;
    else {

        UITouch *touch =[allTouches objectAtIndex:0];
        PointLocation *currentLoc = [[PointLocation alloc] init];
        currentLoc.location = [touch locationInView:self];
        [pointArray addObject:currentLoc];

        [self setNeedsDisplay];
    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSArray *allTouches = [[event allTouches] allObjects];

    if ([allTouches count] > 1)
        return;
    else {

        UITouch *touch =[allTouches objectAtIndex:0];
        PointLocation *currentLoc = [[PointLocation alloc] init];
        currentLoc.location = [touch locationInView:self];

        float xf = currentLoc.location.x;
        float yf = currentLoc.location.y;

        float dx = xf-xi;
        float dy = yf-yi;

        NSLog(@"dx:%f  dy:%f", dx, dy);

        if (dx>50 && dx<100) {
            NSLog(@"enough dx");

            ViewController *anim = [[ViewController alloc]init];
            [anim startAnimation]; //*** this instead doesn't do anything ***

        }
        if (dy>50 && dy<100) {
            NSLog(@"enough dy");
        }

        [pointArray addObject:currentLoc];



        [self setNeedsDisplay];
    }
    NSLog(@"finger lifted");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {

    NSArray *allTouches = [[event allTouches] allObjects];

    if ([allTouches count] > 1)
        return;
    else {
        pointArray = nil;
        [self setNeedsDisplay];
    }
}

Open in new window

I have some PHP, JavaScript and Java knowledge but I'm a few weeks iOS developer wannabe. Your help is really appreciated.

Thanks!
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

In your case, I think, you may call this startAnimation method with a timeout from viewDidLoad, for example:
[self performSelector:@selector(startAnimation) withObject:nil afterDelay:2.0];
Avatar of Hamidreza Vakilian
Hamidreza Vakilian

Hi
Please upload your Xcode project somewhere on Internet so I can take a look and help you efficiently.
Avatar of jledezma

ASKER

Thanks programmer-x,

The project can be downloaded here:

http://wtrns.fr/bIrWKL-I3ytB26o
(link available until September 5th, 2012.)
ASKER CERTIFIED SOLUTION
Avatar of Hamidreza Vakilian
Hamidreza Vakilian

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much.  Excellent solution and even better explanation.