Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5850265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:06:59+00:00 2026-05-22T13:06:59+00:00

I am new to iphone and xcode. This may be a very simple question

  • 0

I am new to iphone and xcode. This may be a very simple question but I could not find an example code in books. I want to do the following:

  • Have 2 UIView on the screen
  • Each view will step through the color of the rainbow (red/orange/yellow/green/blue/indigo/violet) using gesture recognizer, e.g., if the current color is green, if the user swipes up the UIView changes to yellow, if the user swipes down the UIView changes to blue.
  • Hence, each view will need to keep the current color and respond to the swipes accordingly.

I understand how to implement the detection of swipes using gesture recognizer but I don’t know how to have each view keep a separate variable for the current color. I want a generic code because there will be more than 2 UIViews in my application once I figure out how it is done.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T13:07:00+00:00Added an answer on May 22, 2026 at 1:07 pm

    Since you are going to maintain an array of colors, say colorsArray, and assign views their color from that selection. You can do this in the swipe handler.

    - (void)handleSwipe:(UISwipeGestureRecognizer*)swipeGesture {
        UIView *view = swipeGesture.view;
    
        NSInteger currentColorIndex = [colorsArray indexOfObject:view.backgroundColor];
        NSInteger nextColorIndex = currentColorIndex + 1;
        if ( nextColorIndex == [colorsArray count] ) {
            nextColorIndex = 0;
        }
    
        view.backgroundColor = [colorsArray objectAtIndex:nextColorIndex];
    }
    

    This way you don’t need to subclass.

    Subclassing

    You can subclass UIView and add your own instance variables to it. Say,

    @interface RainbowView: UIView {
        NSInteger currentColorIndex;
    }
    
    @property (nonatomic, assign) NSInteger currentColorIndex;
    ...
    
    @end
    
    @implementation RainbowView
    
    @synthesize currentColorIndex;
    
    ...
    
    @end
    

    In your gesture handling method,

    - (void)handleSwipe:(UISwipeGestureRecognizer*)swipeGesture {
        RainbowView *aView = (RainbowView*)swipeGesture.view;
    
        // Get the next color index to aView.currentColorIndex;
        aView.backgroundColor = [colorsArray objectAtIndex:nextColorIndex];
        aView.currentColorIndex = nextColorIndex;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When building a new iPhone app in Xcode, I'm getting this error: codesign error:
I'm new to iPhone development and Xcode, so this might be really obvious. I
This is a new iPhone project, only 1 target (different from this question )
I'm new to iPhone development and Xcode in general and have no idea how
hi i am new to iphone.i am installing the iphone sdk 3.2 with xcode
I am quite new all this iPhone stuff. I have no such clear IDea
I have found this example on StackOverflow: var people = new List<Person> { new
I'm new to iPhone and Apple development and working on my first application. It
I'm new to iPhone development, and multiple views (xib or nib) are really confusing
I'm new to development on the iPhone. Just about every sample project (as well

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.