<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Mac開發基礎17-NSButton(二)

      NSButton是一個功能強大且靈活多樣的控件,除了基本使用和常見API外,還有一些進階用法和技巧可以提高按鈕的可用性和實現細節。在以下內容中,我會詳細介紹一些進階使用技巧,并封裝一個常用的工具類來實現自定義的多種按鈕類型。

      進階使用和技巧

      1. 自定義按鈕的外觀和行為

      Objective-C

      // 自定義按鈕的邊框顏色和寬度
      - (void)customizeButtonAppearance:(NSButton *)button {
          button.wantsLayer = YES;  // 啟用 Core Animation
          button.layer.borderColor = [NSColor redColor].CGColor;  // 設置邊框顏色
          button.layer.borderWidth = 2.0;  // 設置邊框寬度
          button.layer.cornerRadius = 5.0;  // 設置圓角半徑
          button.layer.masksToBounds = YES;  // 避免超出的部分顯示
      }
      

      Swift

      // 自定義按鈕的邊框顏色和寬度
      func customizeButtonAppearance(_ button: NSButton) {
          button.wantsLayer = true  // 啟用 Core Animation
          if let layer = button.layer {
              layer.borderColor = NSColor.red.cgColor  // 設置邊框顏色
              layer.borderWidth = 2.0  // 設置邊框寬度
              layer.cornerRadius = 5.0  // 設置圓角半徑
              layer.masksToBounds = true  // 避免超出的部分顯示
          }
      }
      

      2. 控制按鈕的按下和松開行為

      Objective-C

      - (void)addCustomTrackingToButton:(NSButton *)button {
          NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:button.bounds
                                                                      options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow)
                                                                        owner:self
                                                                     userInfo:nil];
          [button addTrackingArea:trackingArea];  // 添加追蹤區域,用于鼠標進入和離開時的事件處理
      }
      
      - (void)mouseEntered:(NSEvent *)event {
          [self.button setTitle:@"Mouse Entered"];  // 鼠標進入時改變按鈕標題
      }
      
      - (void)mouseExited:(NSEvent *)event {
          [self.button setTitle:@"Mouse Exited"];  // 鼠標離開時恢復按鈕標題
      }
      

      Swift

      func addCustomTrackingToButton(_ button: NSButton) {
          let trackingArea = NSTrackingArea(rect: button.bounds,
                                            options: [.mouseEnteredAndExited, .activeInKeyWindow],
                                            owner: self,
                                            userInfo: nil)
          button.addTrackingArea(trackingArea)  // 添加追蹤區域,用于鼠標進入和離開時的事件處理
      }
      
      override func mouseEntered(with event: NSEvent) {
          self.button.title = "Mouse Entered"  // 鼠標進入時改變按鈕標題
      }
      
      override func mouseExited(with event: NSEvent) {
          self.button.title = "Mouse Exited"  // 鼠標離開時恢復按鈕標題
      }
      

      3. 自定義按鈕圖層屬性

      Objective-C

      - (void)setButtonShadow:(NSButton *)button {
          button.wantsLayer = YES;  // 啟用 Core Animation
          button.layer.shadowColor = [NSColor blackColor].CGColor;  // 設置陰影顏色
          button.layer.shadowOpacity = 0.5;  // 設置陰影透明度
          button.layer.shadowOffset = CGSizeMake(3, -3);  // 設置陰影偏移
          button.layer.shadowRadius = 10.0;  // 設置陰影半徑
      }
      

      Swift

      func setButtonShadow(_ button: NSButton) {
          button.wantsLayer = true  // 啟用 Core Animation
          if let layer = button.layer {
              layer.shadowColor = NSColor.black.cgColor  // 設置陰影顏色
              layer.shadowOpacity = 0.5  // 設置陰影透明度
              layer.shadowOffset = CGSize(width: 3, height: -3)  // 設置陰影偏移
              layer.shadowRadius = 10.0  // 設置陰影半徑
          }
      }
      

      4. 響應鼠標拖動事件

      Objective-C

      - (void)trackMouseDragForButton:(NSButton *)button {
          [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskLeftMouseDragged handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
              NSPoint location = [event locationInWindow];
              [NSCursor.closedHandCursor push];  // 鼠標拖動時改變光標為閉手
              button.frame = NSMakeRect(location.x - button.frame.size.width / 2, 
                                        location.y - button.frame.size.height / 2, 
                                        button.frame.size.width, 
                                        button.frame.size.height);  // 更新按鈕位置
              return event;
          }];
      }
      

      Swift

      func trackMouseDragForButton(_ button: NSButton) {
          NSEvent.addLocalMonitorForEvents(matching: .leftMouseDragged) { event in
              let location = event.locationInWindow
              NSCursor.closedHand.push()  // 鼠標拖動時改變光標為閉手
              button.frame = NSRect(x: location.x - button.frame.size.width / 2,
                                    y: location.y - button.frame.size.height / 2,
                                    width: button.frame.size.width,
                                    height: button.frame.size.height)  // 更新按鈕位置
              return event
          }
      }
      

      5. 動畫效果

      Objective-C

      - (void)animateButton:(NSButton *)button {
          // 創建一個基本動畫來改變按鈕的透明度
          CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
          fadeAnimation.duration = 1.0;  // 動畫持續時間
          fadeAnimation.fromValue = @(1.0);  // 起始狀態
          fadeAnimation.toValue = @(0.0);  // 結束狀態
          fadeAnimation.autoreverses = YES;  // 動畫結束后反轉
          fadeAnimation.repeatCount = HUGE_VALF;  // 無限循環動畫
          
          // 將動畫添加到按鈕的圖層上
          [button.layer addAnimation:fadeAnimation forKey:@"fadeAnimation"];
      }
      

      Swift

      func animateButton(_ button: NSButton) {
          // 創建一個基本動畫來改變按鈕的透明度
          let fadeAnimation = CABasicAnimation(keyPath: "opacity")
          fadeAnimation.duration = 1.0  // 動畫持續時間
          fadeAnimation.fromValue = 1.0  // 起始狀態
          fadeAnimation.toValue = 0.0  // 結束狀態
          fadeAnimation.autoreverses = true  // 動畫結束后反轉
          fadeAnimation.repeatCount = .greatestFiniteMagnitude  // 無限循環動畫
          
          // 將動畫添加到按鈕的圖層上
          button.layer?.add(fadeAnimation, forKey: "fadeAnimation")
      }
      

      封裝工具類

      為了方便創建和管理多種類型的按鈕,我們可以封裝一個工具類。這個工具類可以用于生成不同類型的按鈕,并應用各種自定義設置。

      Objective-C

      #import <Cocoa/Cocoa.h>
      
      @interface NSButtonFactory : NSObject
      
      + (NSButton *)createCustomButtonWithTitle:(NSString *)title target:(id)target action:(SEL)action;
      + (NSButton *)createImageButtonWithImage:(NSImage *)image target:(id)target action:(SEL)action;
      
      @end
      
      @implementation NSButtonFactory
      
      + (NSButton *)createCustomButtonWithTitle:(NSString *)title target:(id)target action:(SEL)action {
          NSButton *button = [[NSButton alloc] init];  // 初始化按鈕
          [button setTitle:title];  // 設置標題
          [button setTarget:target];  // 設置目標
          [button setAction:action];  // 設置動作方法
          [self applyCustomStyleToButton:button];  // 應用自定義樣式
          return button;
      }
      
      + (NSButton *)createImageButtonWithImage:(NSImage *)image target:(id)target action:(SEL)action {
          NSButton *button = [[NSButton alloc] init];  // 初始化按鈕
          [button setImage:image];  // 設置按鈕圖片
          [button setTarget:target];  // 設置目標
          [button setAction:action];  // 設置動作方法
          [self applyCustomStyleToButton:button];  // 應用自定義樣式
          return button;
      }
      
      + (void)applyCustomStyleToButton:(NSButton *)button {
          button.wantsLayer = YES;  // 啟用 Core Animation
          button.layer.borderColor = [NSColor blueColor].CGColor;  // 設置邊框顏色
          button.layer.borderWidth = 1.0;  // 設置邊框寬度
          button.layer.cornerRadius = 4.0;  // 設置圓角半徑
          button.layer.masksToBounds = YES;  // 避免超出的部分顯示
      }
      
      @end
      

      Swift

      import Cocoa
      
      class NSButtonFactory {
          
          static func createCustomButton(title: String, target: AnyObject?, action: Selector?) -> NSButton {
              let button = NSButton()  // 初始化按鈕
              button.title = title  // 設置標題
              button.target = target  // 設置目標
              button.action = action  // 設置動作方法
              applyCustomStyle(to: button)  // 應用自定義樣式
              return button
          }
          
          static func createImageButton(image: NSImage, target: AnyObject?, action: Selector?) -> NSButton {
              let button = NSButton()  // 初始化按鈕
              button.image = image  // 設置按鈕圖片
              button.target = target  // 設置目標
              button.action = action  // 設置動作方法
              applyCustomStyle(to: button)  // 應用自定義樣式
              return button
          }
          
          private static func applyCustomStyle(to button: NSButton) {
              button.wantsLayer = true  // 啟用 Core Animation
              button.layer?.borderColor = NSColor.blue.cgColor  // 設置邊框顏色
              button.layer?.borderWidth = 1.0  // 設置邊框寬度
              button.layer?.cornerRadius = 4.0  // 設置圓角半徑
              button.layer?.masksToBounds = true  // 避免超出的部分顯示
          }
      }
      

      使用示例

      Objective-C

      // 創建自定義標題按鈕
      NSButton *customButton = [NSButtonFactory createCustomButtonWithTitle:@"Custom Button" target:self action:@selector(buttonClicked:)];
      
      // 創建圖片按鈕
      NSButton *imageButton = [NSButtonFactory createImageButtonWithImage:[NSImage imageNamed:@"buttonImage"] target:self action:@selector(buttonClicked:)];
      

      Swift

      // 創建自定義標題按鈕
      let customButton = NSButtonFactory.createCustomButton(title: "Custom Button", target: self, action: #selector(buttonClicked(_:)))
      
      // 創建圖片按鈕
      let imageButton = NSButtonFactory.createImageButton(image: NSImage(named: "buttonImage")!, target: self, action: #selector(buttonClicked(_:)))
      
      posted @ 2024-08-06 16:52  Mr.陳  閱讀(177)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 99热成人精品热久久66| 人人澡人人妻人人爽人人蜜桃 | 制服丝袜中文字幕在线| 97在线碰| 久久精品99国产精品日本| 国产乱子伦农村xxxx| 吴忠市| 在线成人| 狼人大伊人久久一区二区| 免费人欧美成又黄又爽的视频| 狠狠色噜噜狠狠狠狠777米奇| 国产精品久久久久无码av色戒| 日本一区二区三区视频一| 国产AV无码专区亚洲AV漫画| 双乳奶水饱满少妇呻吟免费看| 天天摸天天做天天添欧美| 国产精品成人午夜福利| 国产手机在线αⅴ片无码观看 | 亚洲av成人在线一区| 欧美三级a做爰在线观看 | 92久久精品一区二区| 国产精品视频午夜福利| 国产精品免费视频不卡| 精品午夜福利在线视在亚洲| 久久香蕉国产线看观看猫咪av| 日韩精品理论片一区二区| 天堂网国产| 国产亚洲国产精品二区| 国内精品久久人妻无码妲| 农村熟女大胆露脸自拍| 青神县| 无码专区 人妻系列 在线| 国内少妇偷人精品免费| 看全色黄大色黄大片 视频| 中文字幕av日韩有码| 日韩av综合免费在线| 屁屁影院ccyy备用地址| 国产精品色内内在线播放| 男女性高爱潮免费网站| 精品国产一区二区三区香蕉| 熟女精品国产一区二区三区|