طريقتين عمل لUIButton. تتبع المقبل، وتسعى إلى الأمام:

StackOverflow https://stackoverflow.com/questions/1926702

  •  20-09-2019
  •  | 
  •  

سؤال

وأنا خلق تطبيق مشغل الموسيقى وبحاجة إلى أن 2 الإجراءات ليتم استدعاؤها من زر واحد، واحد للانتقال إلى المسار التالي من خلال اتصال ومتابعة الحدث داخل والبعض لسريع إلى الأمام وطارئ curenttrack من 'الضغط لفترة طويلة ". أنا لا أعرف هذه الحالة يتم الإشارة إلى هذا الضغط لفترة طويلة، اعتقدت أن يكون تهبط، لكنه يعمل فقط أثناء الضغط على زر. عندما تحرير زر، تم تخطي المسار إلى البند التالي. الثابتة والمتنقلة مساعدة

AVAudioPlayer *appSoundPlayer;// declared in .h file

في ملف م، والأسلوب:

-(void)seekForwards{
NSTimeInterval timex;
timex = appSoundPlayer.currentTime;
        timex = timex+5; // forward 5 secs

        appSoundPlayer.currentTime = timex;
        timex = 0;
}
هل كانت مفيدة؟

المحلول

وأنا شخصيا كنت مجرد تتبع الدولة للزر مع عدد صحيح على وحدة تحكم وجهة نظركم أو ضمن زر فرعية. إذا كنت تتبع ما يقوم به زر يمكنك التحكم في كل ما من الإجراءات القيام به. في وضع الخاص بك ملف .h في بعض الاشياء مثل هذا:

enum {
    MyButtonScanning,
    MyButtonStalling,
    MyButtonIdle
};



@interface YourClass : UIViewController {
    NSInteger buttonModeAt;
}
@property (nonatomic) NSInteger buttonModeAt;
-(IBAction)buttonPushedDown:(id)sender;
-(void)tryScanForward:(id)sender;
-(IBAction)buttonReleasedOutside:(id)sender;
-(IBAction)buttonReleasedInside:(id)sender;
@end

وبعد ذلك في الخاص بك رمي ملف .M في بعض من هذه الاشياء:

@implementation YourClass
///in your .m file
@synthesize buttonModeAt;


///link this to your button's touch down
-(IBAction)buttonPushedDown:(id)sender {
    buttonModeAt = MyButtonStalling;
    [self performSelector:@selector(tryScanForward:) withObject:nil afterDelay:1.0];
}

-(void)tryScanForward:(id)sender {
    if (buttonModeAt == MyButtonStalling) {
        ///the button was not released so let's start scanning
        buttonModeAt = MyButtonScanning;

        ////your actual scanning code or a call to it can go here
        [self startScanForward];
    }
}

////you will link this to the button's touch up outside
-(IBAction)buttonReleasedOutside:(id)sender {
    if (buttonModeAt == MyButtonScanning) {
        ///they released the button and stopped scanning forward
        [self stopScanForward];
    } else if (buttonModeAt == MyButtonStalling) {
        ///they released the button before the delay period finished
        ///but it was outside, so we do nothing
    }

    self.buttonModeAt = MyButtonIdle;
}

////you will link this to the button's touch up inside
-(IBAction)buttonReleasedInside:(id)sender {
    if (buttonModeAt == MyButtonScanning) {
        ///they released the button and stopped scanning forward
        [self stopScanForward];
    } else if (buttonModeAt == MyButtonStalling) {
        ///they released the button before the delay period finished so we skip forward
        [self skipForward];
    }

    self.buttonModeAt = MyButtonIdle;

}

وبعد ذلك فقط ربط الإجراءات الزر إلى ما لقد لاحظت في التعليقات قبل IBactions. أنا لم تختبر هذا ولكن ينبغي لها أن تعمل.

نصائح أخرى

ويمكنك فئة فرعية الفئة الزر الخاص بك واللعب قليلا حول أساليب UIResponder ل. على سبيل المثال، في طريقة touchesBegan يمكنك اطلاق النار بعض الموقت واستدعاء الأسلوب الذي سينتقل رمي الملفات ويبطل هذا الموقت في طريقة toucesEnded

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