سؤال

لدي كائن يتحرك من اليمين إلى اليسار عبر الشاشة:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:7.8];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
 myImageview.layer.position = CGPointMake(20,  myImageView.layer.position.y);
[UIView commitAnimations];

لقد اكتشفت أنه حتى أثناء استمرار حدوث الرسوم المتحركة، فإن XCode يحدد بالفعل موقع الصورة كوجهة نهائية لها، ومن أجل اكتشاف لمسة على الصورة المتحركة، أحتاج إلى استخدام طبقة العرض التقديمي:

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

    [super touchesBegan:touches withEvent:event];

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

    if ([myImageview.layer.presentationLayer hitTest:touchPoint]) {
        NSLog(@"it's a hit!");
    }
}

يعمل هذا الجزء.الآن، أود أن تتحرك الصورة للأعلى عند الضغط عليها.أريد أن تتحرك الصورة للأعلى بينما تستمر في حركتها الجانبية.بدلًا من ذلك، يقوم هذا الكود بنقل الصورة ليس للأعلى فحسب، بل أيضًا إلى وجهتها النهائية على اليسار:

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

    [super touchesBegan:touches withEvent:event];

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

    if ([mouse.layer.presentationLayer hitTest:touchPoint]) {
        NSLog(@"it's a hit!");
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        myImageView.layer.position = CGPointMake( mouse.layer.position.x,  myImageView.layer.position.y - 40);
        [UIView commitAnimations];
    }
}

أريد أن تتحرك الصورة للأعلى بينما تستمر في حركتها الجانبية.هل يعرف أحد طريقة لتحقيق ذلك؟

ًشكراً جزيلا!

هل كانت مفيدة؟

المحلول

هل حاولت ضبط خيار الرسوم المتحركة UIViewAnimationOptionBeginFromCurrentState?

(أقول الخيار لأنه خيار في أساليب الرسوم المتحركة القائمة على الكتلة المقدمة مع نظام التشغيل iOS 4.كما أنه متوفر أيضًا [UIView setAnimationBeginsFromCurrentState:YES] إذا لم تتمكن من التبديل بعيدًا عن أساليب فئة UIView المهملة حتى الآن.)

تصبح touchesBegan (باستخدام الرسوم المتحركة المجمعة):

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

    [super touchesBegan:touches withEvent:event];

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

    if ([mouse.layer.presentationLayer hitTest:touchPoint]) {
        NSLog(@"it's a hit!");
        [UIView animateWithDuration:0.5 delay:0.0 options:(UIViewAnimationOptionCurveLinear & UIViewAnimationOptionBeginFromCurrentState) animations:^{
            myImageView.layer.position = CGPointMake( myImageView.layer.position.x,  mouse.layer.position.y - 40);
        }completion:^(BOOL complete){
            //
        }];
    }
}

إذا كنت تستخدم ذلك فيجب أن تكون قادرًا على تحديد الملف النهائي x و y الإحداثيات التي تريدها وجعل الرسوم المتحركة تستمر من النقطة التي تم لمس الكائن عندها إلى هذا الموضع.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top