Просмотр исходного кода

【综合】SKTransitionController补充代理

tangchao 1 год назад
Родитель
Сommit
9bc054fa4d

+ 27 - 5
PDF Office/PDF Master/Class/Common/OC/KMTransitionController/SKTransitionController.h

@@ -61,30 +61,46 @@ typedef NS_ENUM(NSUInteger, SKAnimationTransitionStyle) {
 
 @class CIImage;
 
+extern NSString *KMTransitionStyleName;
+extern NSString *KMDurationName;
+extern NSString *KMShouldRestrictName;
+extern NSString *KMPageTransitionsName;
+extern NSString *KMOldValueName;
+extern NSString *KMNewValueName;
+
+@class SKTransitionController;
+@protocol SKTransitionControllerDelegate <NSObject>
+
+- (void)transitionController:(SKTransitionController *)controller valueDidChanged:(NSDictionary *)info;
+
+@end
+
 @interface SKTransitionController : NSObject {
     NSWindow *window;
     CIImage *initialImage;
     NSRect imageRect;
     
-    SKAnimationTransitionStyle transitionStyle;
-    CGFloat duration;
-    BOOL shouldRestrict;
+    SKAnimationTransitionStyle _transitionStyle;
+    CGFloat _duration;
+    BOOL _shouldRestrict;
     
     SKAnimationTransitionStyle currentTransitionStyle;
     CGFloat currentDuration;
     BOOL currentShouldRestrict;
     BOOL currentForward;
     
-    NSArray *pageTransitions;
+    NSArray *_pageTransitions;
 }
 
-@property (nonatomic, strong) NSView *view;
+@property (nonatomic, weak) NSView *view;
 @property (nonatomic) SKAnimationTransitionStyle transitionStyle;
 @property (nonatomic) CGFloat duration;
 @property (nonatomic) BOOL shouldRestrict;
 @property (nonatomic, copy) NSArray *pageTransitions;
 @property (nonatomic) BOOL hasTransition;
 
+@property (nonatomic, weak) id <SKTransitionControllerDelegate> delegate;
+
 + (NSArray *)transitionNames;
 
 + (NSString *)nameForStyle:(SKAnimationTransitionStyle)style;
@@ -98,3 +114,9 @@ typedef NS_ENUM(NSUInteger, SKAnimationTransitionStyle) {
 - (void)animateForRect:(NSRect)rect;
 
 @end
+
+@interface SKTransitionController (KMExtension)
+
+//@property (nonatomic, weak) id <SKTransitionControllerDelegate> delegate;
+
+@end

+ 73 - 16
PDF Office/PDF Master/Class/Common/OC/KMTransitionController/SKTransitionController.m

@@ -49,11 +49,19 @@
 #include <unistd.h>
 #import <OpenGL/OpenGL.h>
 #import <OpenGL/gl.h>
+#import <objc/runtime.h>
 
 NSString *SKStyleNameKey = @"styleName";
 NSString *SKDurationKey = @"duration";
 NSString *SKShouldRestrictKey = @"shouldRestrict";
 
+NSString *KMTransitionStyleName = @"transitionStyle";
+NSString *KMDurationName = @"duration";
+NSString *KMShouldRestrictName = @"shouldRestrict";
+NSString *KMPageTransitionsName = @"pageTransitions";
+NSString *KMOldValueName = @"oldValue";
+NSString *KMNewValueName = @"newValue";
+
 #define kCIInputBacksideImageKey @"inputBacksideImage"
 #define kCIInputRectangleKey @"inputRectangle"
 
@@ -154,7 +162,8 @@ typedef void(^SKTransitionAnimationProgressHandler)(CGFloat);
 
 @implementation SKTransitionController
 
-@synthesize view, transitionStyle, duration, shouldRestrict, pageTransitions;
+// transitionStyle duration shouldRestrict pageTransitions
+@synthesize view;
 @dynamic hasTransition;
 
 + (NSArray *)transitionNames {
@@ -211,12 +220,12 @@ typedef void(^SKTransitionAnimationProgressHandler)(CGFloat);
 - (id)initForView:(NSView *)aView {
     self = [super init];
     if (self) {
-        view = aView; // don't retain as it may retain us
+        self.view = aView; // don't retain as it may retain us
         imageRect = NSZeroRect;
         
-        transitionStyle = SKNoTransition;
-        duration = 1.0;
-        shouldRestrict = YES;
+        _transitionStyle = SKNoTransition;
+        _duration = 1.0;
+        _shouldRestrict = YES;
         currentTransitionStyle = SKNoTransition;
         currentDuration = 1.0;
         currentShouldRestrict = YES;
@@ -226,8 +235,7 @@ typedef void(^SKTransitionAnimationProgressHandler)(CGFloat);
 }
 
 - (void)dealloc {
-    view = nil;
-   
+    self.view = nil;
 }
 
 - (NSRect)convertRectToScreen:(NSRect)rect view:(NSView *)view{
@@ -237,7 +245,7 @@ typedef void(^SKTransitionAnimationProgressHandler)(CGFloat);
 }
 
 - (BOOL)hasTransition {
-    return transitionStyle != SKNoTransition || pageTransitions != nil;
+    return self.transitionStyle != SKNoTransition || self.pageTransitions != nil;
 }
 
 static inline CGRect scaleRect(NSRect rect, CGFloat scale) {
@@ -340,14 +348,14 @@ static inline CGRect scaleRect(NSRect rect, CGFloat scale) {
 }
 
 - (void)prepareAnimationForRect:(NSRect)rect from:(NSUInteger)fromIndex to:(NSUInteger)toIndex {
-    currentTransitionStyle = transitionStyle;
-    currentDuration = duration;
-    currentShouldRestrict = shouldRestrict;
+    currentTransitionStyle = self.transitionStyle;
+    currentDuration = self.duration;
+    currentShouldRestrict = self.shouldRestrict;
     currentForward = (toIndex >= fromIndex);
     
     NSUInteger idx = MIN(fromIndex, toIndex);
-    if (fromIndex != NSNotFound && toIndex != NSNotFound && idx < [pageTransitions count]) {
-        NSDictionary *info = [pageTransitions objectAtIndex:idx];
+    if (fromIndex != NSNotFound && toIndex != NSNotFound && idx < [self.pageTransitions count]) {
+        NSDictionary *info = [self.pageTransitions objectAtIndex:idx];
         id value;
         if ((value = [info objectForKey:SKStyleNameKey]))
             currentTransitionStyle = [[self class] styleForName:value];
@@ -473,14 +481,51 @@ static inline CGRect scaleRect(NSRect rect, CGFloat scale) {
 	else if (currentTransitionStyle > SKNoTransition && CoreGraphicsServicesTransitionsDefined())
         [self animateUsingCoreGraphics];
     
-    currentTransitionStyle = transitionStyle;
-    currentDuration = duration;
-    currentShouldRestrict = shouldRestrict;
+    currentTransitionStyle = self.transitionStyle;
+    currentDuration = self.duration;
+    currentShouldRestrict = self.shouldRestrict;
     currentForward = YES;
     
     imageRect = NSZeroRect;
 }
 
+- (void)setTransitionStyle:(SKAnimationTransitionStyle)transitionStyle {
+    SKAnimationTransitionStyle oldValue = _transitionStyle;
+    _transitionStyle = transitionStyle;
+    
+    [self.delegate transitionController:self valueDidChanged:@{
+        KMTransitionStyleName : @{
+            KMOldValueName : @(oldValue),
+            KMNewValueName : @(transitionStyle)
+        }
+    }];
+}
+
+- (void)setDuration:(CGFloat)duration {
+    CGFloat oldValue = self.duration;
+    _duration = duration;
+    
+    [self.delegate transitionController:self valueDidChanged:@{
+        KMDurationName : @{
+            KMOldValueName : @(oldValue),
+            KMNewValueName : @(duration)
+        }
+    }];
+}
+
+- (void)setShouldRestrict:(BOOL)shouldRestrict {
+    BOOL oldValue = self.shouldRestrict;
+    _shouldRestrict = shouldRestrict;
+    
+    [self.delegate transitionController:self valueDidChanged:@{
+        KMShouldRestrictName : @{
+            KMOldValueName : @(oldValue),
+            KMNewValueName : @(shouldRestrict)
+        }
+    }];
+}
+
+
 @end
 
 #pragma mark -
@@ -602,3 +647,15 @@ static inline CGRect scaleRect(NSRect rect, CGFloat scale) {
 }
 
 @end
+
+@implementation SKTransitionController (KMExtension)
+
+//- (void)setDelegate:(id<SKTransitionControllerDelegate>)delegate {
+//    objc_setAssociatedObject(self, @selector(delegate), delegate, OBJC_ASSOCIATION_ASSIGN);
+//}
+//
+//- (id<SKTransitionControllerDelegate>)delegate {
+//    return objc_getAssociatedObject(self, @selector(delegate));
+//}
+
+@end

+ 66 - 62
PDF Office/PDF Master/Class/PDFWindowController/MainWindowController/SKPresentationOptionsSheetController.swift

@@ -23,7 +23,7 @@ let TABLE_OFFSET: CGFloat = 8.0
 var SKTransitionPropertiesObservationContext = UnsafeMutableRawPointer(mutating: "SKTransitionPropertiesObservationContext")
 var SKPDFViewTransitionsObservationContext: CChar?
 
-class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate, KMBotaTableViewDelegate, NSTableViewDataSource, NSTableViewDelegate{
+class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate {
     @IBOutlet var notesDocumentPopUpButton: NSPopUpButton!
     @IBOutlet var tableView: KMBotaTableView!
     @IBOutlet var separateCheckButton: NSButton!
@@ -111,6 +111,11 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
 //        transition = KMTransitionInfo()
 //        transitions = nil
 //    }
+    
+    override var windowNibName: NSNib.Name?{
+        return "TransitionSheet"
+    }
+    
     override init(window: NSWindow?) {
         super.init(window: window)
     }
@@ -119,65 +124,16 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
         super.init(coder: coder)
     }
     
-    @objc func handleDocumentsDidChangeNotification(notification: Notification?) {
-        guard let currentDoc = notesDocumentPopUpButton.selectedItem?.representedObject else { return }
-        while notesDocumentPopUpButton.numberOfItems > 1 {
-            notesDocumentPopUpButton.removeItem(at: notesDocumentPopUpButton.numberOfItems - 1)
-        }
-
-        guard let document = controller?.document as? NSDocument,
-              let pageCount = controller?.listView.document?.pageCount else { return }
-
-        let documents = NSMutableArray()
-        for doc in NSDocumentController.shared.documents {
-            if let pdfDoc = doc as? CPDFDocument,
-                doc != document,
-                pdfDoc.pageCount == pageCount {
-                documents.add(doc)
-            }
-        }
-
-        let sortDescriptor = NSSortDescriptor(key: "displayName", ascending: true)
-        documents.sort(using: [sortDescriptor])
-
-        for doc in documents {
-            notesDocumentPopUpButton.addItem(withTitle: (doc as AnyObject).displayName)
-            notesDocumentPopUpButton.lastItem?.representedObject = doc
-        }
-
-        let docIndex = notesDocumentPopUpButton.indexOfItem(withRepresentedObject: currentDoc)
-        notesDocumentPopUpButton.selectItem(at: docIndex == -1 ? 0 : docIndex)
-    }
-    
-    func stopObservingTransitions(infos: [KMTransitionInfo]) {
-//        for info in infos {
-//            info.removeObserver(self, forKeyPath: TRANSITIONSTYLE_KEY) 
-//            info.removeObserver(self, forKeyPath: DURATION_KEY)
-//            info.removeObserver(self, forKeyPath: SHOULDRESTRICT_KEY)
-//        }
-    }
-    
-    func creatTransitionController() {
-        transitionController = SKTransitionController(for: controller?.listView)//controller?.mainViewController.listView.transitionController
-        let options: NSKeyValueObservingOptions = [.new, .old]
-        transitionController?.addObserver(self, forKeyPath: "transitionStyle", options: options, context: &SKPDFViewTransitionsObservationContext)
-        transitionController?.addObserver(self, forKeyPath: "duration", options: options, context: &SKPDFViewTransitionsObservationContext)
-        transitionController?.addObserver(self, forKeyPath: "shouldRestrict", options: options, context: &SKPDFViewTransitionsObservationContext)
-        transitionController?.addObserver(self, forKeyPath: "pageTransitions", options: options, context: &SKPDFViewTransitionsObservationContext)
-    }
-    
     override func windowDidLoad() {
         super.windowDidLoad()
         
         let count = SKTransitionController.transitionNames().count
-//        let transitionStylePopUpButton = transitionControls[0] as! NSPopUpButton
-//        transitionStylePopUpButton.removeAllItems()
         for i in 0..<count {
-            effectPopUpButton.addItem(withTitle: SKTransitionController.localizedName(for: SKAnimationTransitionStyle(rawValue: UInt(i)) ?? .noTransition))
-            effectPopUpButton.lastItem?.tag = i
+            self.effectPopUpButton.addItem(withTitle: SKTransitionController.localizedName(for: SKAnimationTransitionStyle(rawValue: UInt(i)) ?? .noTransition))
+            self.effectPopUpButton.lastItem?.tag = i
         }
 
-        notesDocumentPopUpButton.item(at: 0)?.title = NSLocalizedString("None", comment: "Menu item title")
+        self.notesDocumentPopUpButton.item(at: 0)?.title = NSLocalizedString("None", comment: "Menu item title")
 
         creatTransitionController()
         transition.transitionStyle = transitionController?.transitionStyle ?? .noTransition
@@ -228,6 +184,53 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
         NotificationCenter.default.addObserver(self, selector: #selector(handleDocumentsDidChangeNotification(notification:)), name: NSNotification.Name("SKDocumentControllerDidRemoveDocumentNotification"), object: nil)
     }
     
+    @objc func handleDocumentsDidChangeNotification(notification: Notification?) {
+        guard let currentDoc = notesDocumentPopUpButton.selectedItem?.representedObject else { return }
+        while notesDocumentPopUpButton.numberOfItems > 1 {
+            notesDocumentPopUpButton.removeItem(at: notesDocumentPopUpButton.numberOfItems - 1)
+        }
+
+        guard let document = controller?.document as? NSDocument,
+              let pageCount = controller?.listView.document?.pageCount else { return }
+
+        let documents = NSMutableArray()
+        for doc in NSDocumentController.shared.documents {
+            if let pdfDoc = doc as? CPDFDocument,
+                doc != document,
+                pdfDoc.pageCount == pageCount {
+                documents.add(doc)
+            }
+        }
+
+        let sortDescriptor = NSSortDescriptor(key: "displayName", ascending: true)
+        documents.sort(using: [sortDescriptor])
+
+        for doc in documents {
+            notesDocumentPopUpButton.addItem(withTitle: (doc as AnyObject).displayName)
+            notesDocumentPopUpButton.lastItem?.representedObject = doc
+        }
+
+        let docIndex = notesDocumentPopUpButton.indexOfItem(withRepresentedObject: currentDoc)
+        notesDocumentPopUpButton.selectItem(at: docIndex == -1 ? 0 : docIndex)
+    }
+    
+    func stopObservingTransitions(infos: [KMTransitionInfo]) {
+//        for info in infos {
+//            info.removeObserver(self, forKeyPath: TRANSITIONSTYLE_KEY) 
+//            info.removeObserver(self, forKeyPath: DURATION_KEY)
+//            info.removeObserver(self, forKeyPath: SHOULDRESTRICT_KEY)
+//        }
+    }
+    
+    func creatTransitionController() {
+        transitionController = SKTransitionController(for: controller?.listView)//controller?.mainViewController.listView.transitionController
+        let options: NSKeyValueObservingOptions = [.new, .old]
+        transitionController?.addObserver(self, forKeyPath: "transitionStyle", options: options, context: &SKPDFViewTransitionsObservationContext)
+        transitionController?.addObserver(self, forKeyPath: "duration", options: options, context: &SKPDFViewTransitionsObservationContext)
+        transitionController?.addObserver(self, forKeyPath: "shouldRestrict", options: options, context: &SKPDFViewTransitionsObservationContext)
+        transitionController?.addObserver(self, forKeyPath: "pageTransitions", options: options, context: &SKPDFViewTransitionsObservationContext)
+    }
+    
     func startObservingTransitions(_ infos: [KMTransitionInfo]) {
 //        for info in infos {
 //            info.addObserver(self, forKeyPath: TRANSITIONSTYLE_KEY, options: [.new, .old], context: &SKTransitionPropertiesObservationContext)
@@ -273,9 +276,7 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
         
         transitions = array as NSArray
     }
-    override var windowNibName: NSNib.Name?{
-        return "TransitionSheet"
-    }
+
     
     func changePageTransitionType() {
 //                SKImageToolTipWindow.sharedToolTipWindow().orderOut(nil)
@@ -324,7 +325,6 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
         changePageTransitionType()
     }
     
-    
     @IBAction func changeDurationSlider(_ sender: NSSlider) {
         self.durationTF.stringValue = sender.stringValue
     }
@@ -376,6 +376,10 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
             super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
         }
     }
+    
+}
+
+extension SKPresentationOptionsSheetController: NSTableViewDelegate, NSTableViewDataSource {
     func numberOfRows(in tableView: NSTableView) -> Int {
         return transitions?.count ?? 0
     }
@@ -383,9 +387,9 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
 //        let identifier = tableColumn!.identifier
 //        let info = transitions?[row]
 //        if identifier.rawValue == PAGE_COLUMNID {
-//            
+//
 //        } else if identifier.rawValue == IMAGE_COLUMNID {
-//            
+//
 //        }
 //        return nil
 //    }
@@ -430,6 +434,9 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
         }
         return false
     }
+}
+
+extension SKPresentationOptionsSheetController: KMBotaTableViewDelegate {
     func tableView(_ aTableView: NSTableView, imageContextForRow rowIndex: Int) -> AnyObject? {
         return controller?.document?.page(at: UInt(rowIndex))
     }
@@ -454,6 +461,3 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
         return transitions?.value(forKeyPath: "thumbnail.label") as! NSArray
     }
 }
-
-
-//

+ 3 - 0
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Extension.h

@@ -11,6 +11,7 @@
 //
 
 #import "CPDFListView.h"
+#import "SKTransitionController.h"
 
 @class KMAnnotationStamp, KMSignature;
 @interface CPDFListView (Extension)
@@ -48,6 +49,8 @@
 - (void)resetFormAnnotation;
 - (void)removeAllAnnotation;
 
+@property (nonatomic, readonly) SKTransitionController *transitionController;
+
 @end
 
 @interface CPDFView (KMExtension)

+ 9 - 0
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Extension.m

@@ -796,6 +796,15 @@ CGFloat DEFAULT_SNAPSHOT_HEIGHT = 200.0;
     }
 }
 
+- (SKTransitionController *)transitionController {
+    SKTransitionController *con = objc_getAssociatedObject(self, @selector(transitionController));
+    if (con == nil) {
+        con = [[SKTransitionController alloc] initForView:self];
+        objc_setAssociatedObject(self, @selector(transitionController), con, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+    }
+    return con;
+}
+
 @end
 
 @implementation CPDFView (KMExtension)

+ 0 - 1
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+MenuAction.swift

@@ -345,7 +345,6 @@ extension KMMainViewController {
     }
     
     @IBAction func chooseTransition(_ sender: Any?) {
-        KMPrint("chooseTransition")
 //        presentationSheetController = [[SKPresentationOptionsSheetController alloc] initForController:self];
 //
 //        [presentationSheetController beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {

+ 3 - 0
PDF Office/PDF Master/Class/README.md

@@ -44,6 +44,9 @@
 ## 信息
 * KMProfileInfoWindowController
 
+## 幻灯片
+* 选项设置 SKPresentationOptionsSheetController
+
 ## xxx
 
 [sign in](https://www.baidu.com) the [dashboard](https://www.baidu.com). to [sign up](https://www.baidu.com) 

+ 4 - 4
PDF Office/PDF Reader Pro.xcodeproj/xcuserdata/kdanmobile.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -301,8 +301,8 @@
             filePath = "PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+MenuAction.swift"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "947"
-            endingLineNumber = "947"
+            startingLineNumber = "946"
+            endingLineNumber = "946"
             landmarkName = "autoCropAll(_:)"
             landmarkType = "7">
          </BreakpointContent>
@@ -317,8 +317,8 @@
             filePath = "PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+MenuAction.swift"
             startingColumnNumber = "9223372036854775807"
             endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "952"
-            endingLineNumber = "952"
+            startingLineNumber = "951"
+            endingLineNumber = "951"
             landmarkName = "smartAutoCropAll(_:)"
             landmarkType = "7">
          </BreakpointContent>