Hanny
Hanny

Reputation: 1342

check on images displayed in UIScrollview

Hi i am new to IPhone development.
I am creating UIImageViews programmatically on button click. but every time i click the button they get drawn at the same place. the image drawing code is as follows.

- (IBAction)Button
{
arrayOfImages = [[NSMutableArray alloc]init];
float x = 15.0,y = 15.0, width = 100.0, height = 100;
CGRect frame = CGRectMake(x, y, width, height);
int i= 0;

if ([shape isEqualToString:@"Rectangle"])
{

    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);    
    CGRect main =CGRectMake(x, y, 70.0, 30.0);
    CGContextFillRect(context, main);   

    CGRect topSeat1 = CGRectMake(15.0, 0.0, 15.0, 13.0);
    CGRect topSeat2 = CGRectMake(42.5, 0.0, 15.0, 13.0);
    CGRect topSeat3 = CGRectMake(70.0, 0.0, 15.0, 13.0);

    CGRect leftSeat = CGRectMake(0.0, 22.5, 13.0, 15.0);

    CGRect rightSeat = CGRectMake(87.0, 22.5, 13.0, 15.0);


    [[UIColor redColor]set];
    //UIRectFill(main);
    UIRectFill(topSeat1);
    UIRectFill(topSeat2);
    UIRectFill(topSeat3);
    UIRectFill(leftSeat);
    UIRectFill(rightSeat);
    UIRectFrame(main);

    [[UIColor blackColor]set];
    UIRectFrame(topSeat1);
    UIRectFrame(topSeat2);
    UIRectFrame(topSeat3);
    UIRectFrame(leftSeat);
    UIRectFrame(rightSeat);

    UIImage * images = [[UIImage alloc]init];
    images = UIGraphicsGetImageFromCurrentImageContext();

    UIImageView* myImage=[[UIImageView alloc]initWithFrame:frame];
    [myImage setImage:images];
    myImage.tag= i;
    i++;


    UIGraphicsEndImageContext();
    [self.view addSubview:myImage];
    [arrayOfImages addObject:myImage];
    [myImage setUserInteractionEnabled:YES];
}
}  

all i want is that if i click the button it should check that if there is already UIIMAgeView drawn at that place then it draws the UIIMageView a bit away from it.
I would appreciate if u help me with some code.

Upvotes: 0

Views: 383

Answers (1)

Bryan Chen
Bryan Chen

Reputation: 46588

use a variable to store the click count of the button, then use the variable to adjust the place of your view

or you may ask for the superview for subviews and compare the frame of the subview with your image view

for (UIView *subview in self.view.subviews) {
    if (/* compare subview.frame with myImage.frame */) {
        /* update myImage.frame */
        break;
    }
}

Upvotes: 2

Related Questions