检查UIScrollview中显示的图像

时间:2011-03-07 09:45:15

标签: iphone uiscrollview uiimageview uibutton

您好我是iPhone开发的新手 我在按钮单击时以编程方式创建UIImageViews。但每次我点击按钮,他们都会在同一个地方画出来。图像绘制代码如下。

- (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];
}
}  

我想要的是,如果我点击按钮,它应该检查是否已经在那个地方绘制了UIIMAgeView,然后它将UIIMageView拉远一点。
 如果你帮我一些代码,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

使用变量存储按钮的点击次数,然后使用变量调整视图的位置

或者您可以要求子视图的超级视图并将子视图的框架与图像视图进行比较

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