Различать два вида изображений

#objective-c #cocoa-touch #ipad #uiimageview

#objective-c #cocoa-touch #iPad #uiimageview

Вопрос:

У меня есть два просмотра изображений ImageView1 и imageView2. Я дал GestureRecognizer для перемещения этих обоих изображений. Теперь проблема в том, что когда я перемещаю первый вид изображения рядом со вторым видом изображения, отображается только второй вид изображения. Но когда я помещаю первый просмотр изображения на другое изображение, первый просмотр изображения должен отображаться на втором просмотре изображения, чего не происходит.Может ли кто-нибудь, пожалуйста, сказать мне, как первый просмотр изображения отображается поверх другого просмотра изображения.

Ответ №1:

Вы можете использовать UIView метод bringSubviewToFront: и передать UIImageView тот, который вы хотите отобразить сверху.

Ответ №2:

Почему бы вам не использовать что-то подобное для перетаскивания ваших UIImageViews?

 - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor yellowColor];

    test1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    test1.backgroundColor = [UIColor whiteColor];
    test1.userInteractionEnabled = YES;
    test1.clipToBounds = YES;
    [self.view addSubview:test1];

    test2 = [[UIImageView alloc] initWithFrame:CGRectMake(400, 400, 100, 100)];
    test2.backgroundColor = [UIColor whiteColor];
    test2.userInteractionEnabled = YES;
    test2.clipToBounds = YES;
    [self.view addSubview:test2];
}


CGPoint startLocation;
float diffX;
float diffY;

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

    if( [touch view] == test1)
    {
        startLocation = [touch locationInView:self.view];
        [self.view bringSubviewToFront:test1];
    }

    if( [touch view] == test2)
    {
        startLocation = [touch locationInView:self.view];
        [self.view bringSubviewToFront:test2];
    }
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [[event allTouches] anyObject];

    if( [touch view] == test1)
    {
        CGPoint location = [touch locationInView:self.view];
        diffX = location.x - startLocation.x;
        diffY = location.y - startLocation.y;
        test1.frame = CGRectMake(test1.frame.origin.x   diffX, test1.frame.origin.y   diffY, test1.frame.size.width, test1.frame.size.height);
        startLocation = test1.frame.origin;
    }

    if( [touch view] == test2)
    {
        CGPoint location = [touch locationInView:self.view];
        diffX = location.x - startLocation.x;
        diffY = location.y - startLocation.y;
        test2.frame = CGRectMake(test2.frame.origin.x   diffX, test2.frame.origin.y   diffY, test2.frame.size.width, test2.frame.size.height);
        startLocation = test2.frame.origin;
    }

}
  

//—РЕДАКТИРОВАТЬ—//
Добавлено: cliptobounds в viewdidload

Комментарии:

1. Он работает абсолютно нормально, но он не обрезается, когда приближается ко второму просмотру изображений.

2. вы установили свойство ClipToBounds (BOOL)?

3. Да, я установил для него значение «да» в методе перемещения касаний. Но все же у меня та же проблема.

4. сделайте это в viewdidload и отправьте мне результаты

5. Я установил это свойство для обоих просмотров изображений в viewDidLoad, но у меня такая же проблема.