0

In my UIScrollView,I have 30 images in my array. I tried getting the objectAtIndex in my array. I want to be able to do is display an animation in a specific image when UIScrollView stop scrolling. So I tried this code below:

- (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    if([images objectAtIndex:0]){

        //Animation 1

    } else if([images objectAtIndex:1]){

        //Animation 2

    } else if{
...

}

But I cant make it do my animation. Is my condition possible or is there other way?

2 Answers 2

2

Your code:

if([images objectAtIndex:0]) 

will evaluate if the object stored in the array images at index position 0 is nil or not. And presumably if the array is in bounds then it will return true. And if the array is out of bounds then that statement will cause a crash. So the result of your if statements will most likely be true or crash (unless you are storing nil pointers in the array for some reason).

What are you trying to evaluate? Are you trying to determine what image is showing when your scroll view stops scrolling?

6
  • yes. Im trying to determine what image is showing when your scroll view stops scrolling.
    – Bazinga
    May 17, 2012 at 3:46
  • What I mean is I want to make a condition that when a specific image it will execute the action that corresponds to its condition.
    – Bazinga
    May 17, 2012 at 3:54
  • 1
    if you want to determine where you are when it stops scrolling, then use contentOffset and contentSize properties of the UIScrollview. Then depending on how your images are laid out in the scroll view, you can calculate what is showing when scrolling stops.
    – Joel
    May 17, 2012 at 3:56
  • How to implement it? Im not familiar with contentOffset and contentSize. Thanks for the help
    – Bazinga
    May 17, 2012 at 4:01
  • 1
    Read the documentation. If you don't know what contentSize is, then I have a feeling that your UIScrollView is probably not setup correctly in the first place as you need to set the contentSize for it to work properly. The contentOffset will tell you what the user is seeing within your scroll view (and then you can calculate what image that would be with simple math).
    – Joel
    May 17, 2012 at 4:11
0

Im applying NSNumber or NSString to my array. Like this:

for (int i=0; i<30;i++)
{

    //for NSString
    [arrayName addObject:[NSString stringWithFormat:@"%d", i]];

    //for  NSNumber
    [arrayName addObject:[NSNumber numberWithInt:i]];
}

Then in your scrollViewDidEndScrollingAnimation,implement either of this condition:

if ([[arrayName objectAtIndex:anIndex] intValue] == 1)

if ([arrayName objectAtIndex:anIndex] isEqualToString:@"1"])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.