Make Images appear

Discussion in 'iOS Development' started by startrees, Aug 4, 2010.

  1. startrees

    startrees Member

    Joined:
    Nov 29, 2008
    Messages:
    223
    Likes Received:
    0
    Device:
    iPad 2 (White)
    So I was going to make an app where you just catch the things that fall... I was wondering how you make the stuff come on the screen fall then stay on the screen and have another one fall.
    THANKS!
  2. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    Dude, you are asking us to make your app.
  3. Just_For_Now

    Just_For_Now Active Member

    Joined:
    Mar 21, 2009
    Messages:
    1,898
    Likes Received:
    1
    Device:
    4G iPod touch
    I bet he has no idea where to start.
  4. Freerunnering

    Freerunnering Member

    Joined:
    Sep 1, 2009
    Messages:
    296
    Likes Received:
    0
    Device:
    4G iPod touch
  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Kinf of like Connect4. You drop a piece, it stays until the board is reset. Pieces pile on top of each other.

    Take a look at this source code here for exactly that. http://github.com/SkylarEC/ConnectSome I show two different ways to handle this exact scenario. One verision uses UIKit, the other version uses CoreGraphice. Both versions benchmark roughly the same. By default, the app uses CG. To see the same thing with UIKit, uncomment out the line the code tells you to uncomment change the TEST_CHECKERDROP_WITH_UIVIEWANIMATIONS flag to true.


    This is out of context, so you'll want to take a look at the whole project to see the entire picture, but here's the source code:
    [OBJC]//
    // Column.m
    // ConnectSome
    //
    // Created by Skylar Cantu on 11/19/09.
    // Copyright 2009 Skylar Cantu. All rights reserved.
    //

    #import "Column.h"

    #define PADDING 5
    #define CHECKER_FLUFFING 2 /*Required to offset my failure to correctly PhotoShop a checker.*/
    #define FALLING_CHECKER_TAG 100
    #define ADJUSTED_CHECKER_CENTER CGPointMake(checker.center.x, checker.center.y + PADDING + [self rowHeight])
    #define ADD_CHECKER_ANIMATION @"AddCheckerAnimation"
    #define REMOVE_CHECKER_ANIMATION @"RemoveCheckerAnimation"
    #define ANIMATION_KEY @"position"
    #define TEST_CHECKERDROP_WITH_UIVIEWANIMATION_KEY @"TestingCheckerDropWithUIViewAnimation"

    @interface Column (PrivateMethods)
    - (void)addPathToChecker

    Please Register or Log in to view images

    Checker *)checker alongPath

    Please Register or Log in to view images

    CGPathRef)path withSpeed

    Please Register or Log in to view images

    float)seconds;
    - (void)animationDidStop

    Please Register or Log in to view images

    CAAnimation *)animation finished

    Please Register or Log in to view images

    BOOL)finished;
    - (void)animationDidStop

    Please Register or Log in to view images

    NSString *)animationID finished

    Please Register or Log in to view images

    NSNumber *)finished context

    Please Register or Log in to view images

    void *)context;
    - (float)determineCheckerDiameter;
    - (float)rowHeight;
    @end

    @implementation Column

    @synthesize numberOfPiecesInColumn = _numberOfPiecesInColumn, checkerDiameter = _checkerDiameter;
    @synthesize checkerPadding = _checkerPadding, rows = _rows, delegate = _delegate;

    - (id)initWithFrame

    Please Register or Log in to view images

    CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    _numberOfPiecesInColumn = 0;
    _checkerDiameter = self.bounds.size.width;
    }
    return self;
    }

    - (void)addCheckerWithType

    Please Register or Log in to view images

    CSCheckerType)checkerType {
    _checkerDiameter = [self determineCheckerDiameter];
    Checker *checker = [[Checker alloc] initWithCheckerType:checkerType];
    checker.frame = CGRectMake(0, 0, _checkerDiameter, _checkerDiameter);
    checker.center = CGPointMake(self.bounds.origin.x - CHECKER_FLUFFING + PADDING / 2 + _checkerDiameter / 2, 0 - _checkerDiameter / 2);
    checker.tag = FALLING_CHECKER_TAG;
    [self addSubview:checker];

    repeat = _rows - _numberOfPiecesInColumn;
    _numberOfPiecesInColumn++;

    CGRect finalRect = CGRectMake(checker.frame.origin.x, (checker.frame.origin.y + [self rowHeight] * repeat) + PADDING * (repeat - 1), checker.frame.size.width, checker.frame.size.height);
    #if TEST_CHECKERDROP_WITH_UIVIEWANIMATIONS
    [UIView beginAnimations:TEST_CHECKERDROP_WITH_UIVIEWANIMATION_KEY context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector

    Please Register or Log in to view images

    selector(animationDidStop:finished:context

    Please Register or Log in to view images

    ];
    [UIView setAnimationDuration:COLUMN_GRAVITY * repeat];
    //checker.frame = finalRect;
    #else
    CGPoint finalPoint = CGPointMake(finalRect.origin.x + _checkerDiameter / 2, finalRect.origin.y + _checkerDiameter / 2);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, checker.center.x, checker.center.y);
    CGPathAddLineToPoint(path, nil, finalPoint.x, finalPoint.y);
    [self addPathToChecker:checker alongPath

    Please Register or Log in to view images

    ath withSpeed:COLUMN_GRAVITY * repeat];
    CGPathRelease(path);
    #endif
    [checker setFrame:finalRect];
    #if TEST_CHECKERDROP_WITH_UIVIEWANIMATIONS
    [UIView commitAnimations];
    #endif

    [checker release];
    }

    - (void)removePiecesFromBoard {
    for (UIView *view in self.subviews) {
    [view removeFromSuperview];
    _numberOfPiecesInColumn = 0;
    }
    }


    - (void)dropPiecesFromBoard {
    #if MANUALLY_DROP_CHECKERS
    repeat = _rows;
    [UIView beginAnimations:REMOVE_CHECKER_ANIMATION context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector

    Please Register or Log in to view images

    selector(animationDidStop:finished:context

    Please Register or Log in to view images

    ];
    [UIView setAnimationDuration:COLUMN_GRAVITY * 2];
    for(UIView *checker in self.subviews) {
    checker.center = ADJUSTED_CHECKER_CENTER;
    }
    [UIView commitAnimations];
    #else
    [self removePiecesFromBoard];
    #endif
    }


    #pragma mark
    #pragma mark PrivateMethods

    - (void)addPathToChecker

    Please Register or Log in to view images

    Checker *)checker alongPath

    Please Register or Log in to view images

    CGPathRef)path withSpeed

    Please Register or Log in to view images

    float)seconds {
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animation];
    animation.calculationMode = kCAAnimationPaced;
    animation.delegate = self;
    animation.duration = seconds;
    animation.fillMode = kCAFillModeForwards;
    animation.path = path;
    animation.repeatCount = 0;

    [[checker layer] addAnimation:animation forKey:ANIMATION_KEY];
    }

    - (void)animationDidStop

    Please Register or Log in to view images

    CAAnimation *)animation finished

    Please Register or Log in to view images

    BOOL)finished {
    if ([[(CAKeyframeAnimation *)animation keyPath] isEqualToString:ANIMATION_KEY]) {
    if (_delegate != nil && [_delegate respondsToSelector

    Please Register or Log in to view images

    selector(column:didFinishPlacingChecker

    Please Register or Log in to view images

    ]) {
    [_delegate column:self didFinishPlacingChecker:finished];
    }
    }
    }

    - (void)animationDidStop

    Please Register or Log in to view images

    NSString *)animationID finished

    Please Register or Log in to view images

    NSNumber *)finished context

    Please Register or Log in to view images

    void *)context {
    #if TEST_CHECKERDROP_WITH_UIVIEWANIMATIONS
    if ([animationID isEqualToString:TEST_CHECKERDROP_WITH_UIVIEWANIMATION_KEY]) {
    if (_delegate != nil && [_delegate respondsToSelector

    Please Register or Log in to view images

    selector(column:didFinishPlacingChecker

    Please Register or Log in to view images

    ]) {
    [_delegate column:self didFinishPlacingChecker:[finished boolValue]];
    }
    }
    #endif
    if ([animationID isEqualToString:ADD_CHECKER_ANIMATION]) {
    UIView *checker;
    for (UIView *view in self.subviews) {
    if (view.tag = FALLING_CHECKER_TAG) {
    checker = view;
    }
    }

    if (repeat > 0) {
    repeat--;
    [UIView beginAnimations:ADD_CHECKER_ANIMATION context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector

    Please Register or Log in to view images

    selector(animationDidStop:finished:context

    Please Register or Log in to view images

    ];
    [UIView setAnimationDuration:COLUMN_GRAVITY];
    checker.center = ADJUSTED_CHECKER_CENTER;
    [UIView commitAnimations];
    } else {
    checker.tag = 0;
    }
    }
    #if MANUALLY_DROP_CHECKERS
    else if ([animationID isEqualToString:REMOVE_CHECKER_ANIMATION]) {
    if (repeat > 0) {
    repeat--;
    [UIView beginAnimations:REMOVE_CHECKER_ANIMATION context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector

    Please Register or Log in to view images

    selector(animationDidStop:finished:context

    Please Register or Log in to view images

    ];
    [UIView setAnimationDuration:COLUMN_GRAVITY * 2];
    for(UIView *checker in self.subviews) {
    if (!checker.center.y > self.frame.size.height) {
    checker.center = ADJUSTED_CHECKER_CENTER;
    } else {
    [checker removeFromSuperview];
    _numberOfPiecesInColumn--;
    }
    }
    [UIView commitAnimations];
    }
    }
    #endif

    }

    - (float)determineCheckerDiameter {
    float columnWidth = self.bounds.size.width;
    CGFloat rowHeight = [self rowHeight] + CHECKER_FLUFFING;
    return columnWidth < rowHeight ? columnWidth : rowHeight;
    }

    - (float)rowHeight {
    CGFloat adjustedHeight = self.bounds.size.height / _rows;
    adjustedHeight -= PADDING;
    return adjustedHeight;
    }


    @end

    [/OBJC]
  6. startrees

    startrees Member

    Joined:
    Nov 29, 2008
    Messages:
    223
    Likes Received:
    0
    Device:
    iPad 2 (White)
    THANKS!! That was a big help for me!
  7. startrees

    startrees Member

    Joined:
    Nov 29, 2008
    Messages:
    223
    Likes Received:
    0
    Device:
    iPad 2 (White)
    What does "..".tag mean? I have never seen it before
  8. MountainDew

    MountainDew New Member

    Joined:
    Feb 23, 2008
    Messages:
    194
    Likes Received:
    1
    Device:
    iPod touch
    From UIView:
  9. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    You are very intelligent, aren't you? I bet your parents are really proud.

    Obviously he has no idea where to start. Why else do you think he is asking for help? A forum is for help, not for trying to making fun of those who are learning.

Share This Page