触摸与多个点相交后如何更改图像?

How do I make an image change after touch intersects multiple points?

希望我对问题的措辞正确。目前我有以下代码:

-(void)changeImage 
{
  if (CGRectContainsPoint(star1.frame, lastPoint)){
     image.image = [UIImage imageNamed:@"first.png"];
  }
}

代码会在 mouse/finger 触及 star1.frame 时更改图像。我希望它仅在不按特定顺序触及 star1.frame、star2.frame 和 star3.frame(全部三个)时更改。


查看Controller.h

@interface ViewController : UIViewController {


CGPoint lastPoint;
CGPoint moveBackTo;
CGPoint currentPoint;
CGPoint location;
NSDate*lastClick;
BOOL mouseSwiped;
UIImageView *drawImage2;
UIImageView *frontImage2;

    IBOutlet UIImageView *letter;
IBOutlet UIImageView *star1;
IBOutlet UIImageView *star2;





}

@property BOOL hadTouchedOne;
@property BOOL hadTouchedTwo;

-(void)changeImage;

然后是完整的.m

@实施ViewController

- (void)viewDidLoad {

        letter.image = [UIImage imageNamed:@"A.png"];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
drawImage2.image = [defaults objectForKey:@"drawImageKey"];
drawImage2 = [[UIImageView alloc] initWithImage:nil];
drawImage2.frame = self.view.frame;
[self.view addSubview:drawImage2];


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];

if ([touch tapCount] == 3 ) {
    drawImage2.image = nil;
}
location = [touch locationInView: touch.view];
lastClick = [NSDate date];

lastPoint = [touch locationInView: self.view];
lastPoint.y -= 0;

[super touchesBegan: touches withEvent: event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;

UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView: self.view];

UIGraphicsBeginImageContext(CGSizeMake(1028, 1028));
[drawImage2.image drawInRect:CGRectMake(0, 0, 1028, 1028)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 26/255.0f, 188/255.0f, 156/255.0f, 1);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
 CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x,       currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());

[drawImage2 setFrame: CGRectMake(0, 0, 1028, 1028)];
drawImage2.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;

[self.view addSubview:drawImage2];
[self changeImage];


}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}




-(void)changeImage {

if (CGRectContainsPoint(star1.frame, lastPoint)) self.hadTouchedOne = YES;
if (CGRectContainsPoint(star2.frame, lastPoint)) self.hadTouchedOne = YES;


if (_hadTouchedOne && _hadTouchedTwo) {
            letter.image = [UIImage imageNamed:@"B.png"];


}

}

编辑:

@property BOOL hadTouchedOne;
@property BOOL hadTouchedTwo;
@property BOOL hadTouchedThree;

if (CGRectContainsPoint(star1.frame, lastPoint)){
    self.hadTouchedOne = YES;
}
else if (CGRectContainsPoint(star2.frame, lastPoint)){
    self.hadTouchedTwo = YES;
}
if (_hadTouchedOne && _hadTouchedTwo) {
    [letter setImage:[UIImage imageNamed:@"B.png"]];
}

在这种情况下,我使用else if来触发标志。 我注意到您的代码中有一个拼写错误,您的代码中有两个 hadTouchedOne。现在,它可能会起作用。