Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加ObjectiveC版本 #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions PaperSwitch/PaperSwitch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// PaperSwitch.h
// XKSCommonSDK
//
// Created by _Finder丶Tiwk on 16/1/26.
// Copyright © 2016年 _Finder丶Tiwk. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface PaperSwitch : UISwitch

/*! 动画时间 默认为0.25秒*/
@property (nonatomic,assign) CFTimeInterval duration;
/*! 展开画面填充颜色,如果不设置,为onTintColor 如果onTintColor为空则为绿色*/
@property (nonatomic,strong) UIColor *fillColor;

/*! 动画开始回调,从block中的animation参数 可以取到动画相关信息*/
@property (nonatomic,copy) void (^animationStartBlock)(CAAnimation *animation);
/*! 动画结束回调,从block中的animation参数 可以取到动画相关信息,complete标识动画是否结束*/
@property (nonatomic,copy) void (^animationStopBlock)(CAAnimation *animation,BOOL complete);

@end
139 changes: 139 additions & 0 deletions PaperSwitch/PaperSwitch.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//
// PaperSwitch.m
// XKSCommonSDK
//
// Created by _Finder丶Tiwk on 16/1/26.
// Copyright © 2016年 _Finder丶Tiwk. All rights reserved.
//

#import "PaperSwitch.h"

@interface PaperSwitch ()
@property (nonatomic,strong) CAShapeLayer *shapLayer;
@end

@implementation PaperSwitch{
BOOL _status; /**< Switch开关初始状态*/
BOOL _codeInit; /**< 是否是通过代码创建的*/
}

#pragma mark - Accessor
static NSString *kShapeLayerName = @"circleShape";
- (CAShapeLayer *)shapLayer{
if (!_shapLayer) {
_shapLayer = [CAShapeLayer layer];
_shapLayer.name = kShapeLayerName;
_shapLayer.masksToBounds = YES;
}
return _shapLayer;
}

- (CFTimeInterval)duration{
return _duration > 0 ? :0.25;
}

- (UIColor *)fillColor{
return _fillColor? : (self.onTintColor?:[UIColor greenColor]);
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
_codeInit = YES;
}
return self;
}

- (void)awakeFromNib{
[super awakeFromNib];
[self addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[self.superview.layer insertSublayer:self.shapLayer atIndex:0];
self.superview.layer.masksToBounds = YES;
}


#pragma mark - 重写UISwitch设置状态方法
- (void)setOn:(BOOL)on animated:(BOOL)animated{
[super setOn:on animated:animated];
_status = on;
}

- (CATransform3D)scaleTransform:(BOOL)clockwise{
CGFloat scale = clockwise?0.0001:1.0;
return CATransform3DMakeScale(scale,scale,scale);
}

- (void)layoutSubviews{
if (_codeInit) {
/*
* 如果是通过代码进行初始化的要等到它加入到SuperView之后,
* 才能找到SuperView为其添加ShapeLayer。
*/
BOOL added = NO;
for (CAShapeLayer *layer in self.superview.layer.sublayers) {
if ((added = [layer.name isEqualToString:kShapeLayerName])) {
break;
}
}
if (!added) {
[self.superview.layer insertSublayer:self.shapLayer atIndex:0];
self.superview.layer.masksToBounds = YES;
}
}

CGFloat midX = self.center.x;
CGFloat midY = self.center.y;

CGFloat x = MAX(midX, self.superview.frame.size.width - midX);
CGFloat y = MAX(midY, self.superview.frame.size.height - midY);

CGFloat radius = sqrt(pow(x, 2) + pow(y, 2));
self.shapLayer.transform = [self scaleTransform:!_status];
self.shapLayer.fillColor = self.fillColor.CGColor;
self.shapLayer.frame = (CGRect){{midX-radius,midY-radius},{radius*2,radius*2}};
self.shapLayer.anchorPoint = (CGPoint){0.5,0.5};
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:(CGRect){CGPointZero,{radius*2,radius*2}}];
self.shapLayer.path = bezierPath.CGPath;
}


- (void)switchChanged:(PaperSwitch *)sender{
_status = sender.on;
NSValue *from = [NSValue valueWithCATransform3D:[self scaleTransform:_status]];
NSValue *to = [NSValue valueWithCATransform3D:[self scaleTransform:!_status]];

NSString *animationKey1 = @"ZoomIn";
NSString *animationKey2 = @"ZoomOut";
NSString *removeKey = _status?animationKey2:animationKey1;
NSString *addKey = _status?animationKey1:animationKey2;
NSString *timingFunction = _status?kCAMediaTimingFunctionEaseIn:kCAMediaTimingFunctionEaseOut;

[CATransaction begin];
[self.shapLayer removeAnimationForKey:removeKey];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.fromValue = from;
animation.toValue = to;
animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction];
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.duration = self.duration;
animation.delegate = self;
[self.shapLayer addAnimation:animation forKey:addKey];
[CATransaction commit];
}

#pragma mark - CAAnimationDelegate
- (void)animationDidStart:(CAAnimation *)anim{
if (self.animationStartBlock) {
self.animationStartBlock(anim);
}
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
if (flag && self.animationStopBlock) {
self.animationStopBlock(anim,flag);
}
}
@end
5 changes: 5 additions & 0 deletions PaperSwitch/PaperSwitchDemo-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "PaperSwitch.h"
15 changes: 14 additions & 1 deletion PaperSwitchDemo/PaperSwitchDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
1D2EFAB01C58BA7A00948D1A /* PaperSwitch.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D2EFAAF1C58BA7A00948D1A /* PaperSwitch.m */; };
846E0ED31C464B5C0052CDD8 /* Launch.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 846E0ED21C464B5C0052CDD8 /* Launch.storyboard */; };
9C688A001A274993008BFF1E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9C6889FF1A274993008BFF1E /* AppDelegate.swift */; };
9C688A021A274993008BFF1E /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9C688A011A274993008BFF1E /* ViewController.swift */; };
Expand All @@ -27,6 +28,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1D2EFAAE1C58BA7A00948D1A /* PaperSwitch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PaperSwitch.h; sourceTree = "<group>"; };
1D2EFAAF1C58BA7A00948D1A /* PaperSwitch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PaperSwitch.m; sourceTree = "<group>"; };
1DF9DCBD1C570F1E00EADE5C /* PaperSwitchDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "PaperSwitchDemo-Bridging-Header.h"; sourceTree = "<group>"; };
846E0ED21C464B5C0052CDD8 /* Launch.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Launch.storyboard; sourceTree = "<group>"; };
9C6889FA1A274993008BFF1E /* PaperSwitchDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PaperSwitchDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
9C6889FE1A274993008BFF1E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -118,7 +122,10 @@
9C688A211A274A39008BFF1E /* PaperSwitch */ = {
isa = PBXGroup;
children = (
1D2EFAAE1C58BA7A00948D1A /* PaperSwitch.h */,
1D2EFAAF1C58BA7A00948D1A /* PaperSwitch.m */,
9C688A221A274A39008BFF1E /* RAMPaperSwitch.swift */,
1DF9DCBD1C570F1E00EADE5C /* PaperSwitchDemo-Bridging-Header.h */,
);
name = PaperSwitch;
path = ../../PaperSwitch;
Expand Down Expand Up @@ -169,7 +176,7 @@
isa = PBXProject;
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = Ramotion;
TargetAttributes = {
Expand Down Expand Up @@ -228,6 +235,7 @@
files = (
9C688A021A274993008BFF1E /* ViewController.swift in Sources */,
9C688A231A274A39008BFF1E /* RAMPaperSwitch.swift in Sources */,
1D2EFAB01C58BA7A00948D1A /* PaperSwitch.m in Sources */,
9C688A001A274993008BFF1E /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -335,23 +343,28 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
INFOPLIST_FILE = PaperSwitchDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "ramotion.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "/Users/huafan/Desktop/GithubRepository/paper-switch/PaperSwitch/PaperSwitchDemo-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
name = Debug;
};
9C688A1B1A274993008BFF1E /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
INFOPLIST_FILE = PaperSwitchDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "ramotion.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "/Users/huafan/Desktop/GithubRepository/paper-switch/PaperSwitch/PaperSwitchDemo-Bridging-Header.h";
};
name = Release;
};
Expand Down
41 changes: 30 additions & 11 deletions PaperSwitchDemo/PaperSwitchDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,59 @@

import UIKit


class ViewController: UIViewController {

@IBOutlet weak private var connectContactsLabel: UILabel!
@IBOutlet weak private var phone1ImageView: UIImageView!
@IBOutlet weak private var paperSwitch1: RAMPaperSwitch!
// @IBOutlet weak private var paperSwitch1: RAMPaperSwitch!
@IBOutlet weak private var paperSwitch1: PaperSwitch!

@IBOutlet weak private var allowDiscoveryLabel: UILabel!
@IBOutlet weak private var phone2ImageView: UIImageView!
@IBOutlet weak private var paperSwitch2: RAMPaperSwitch!
// @IBOutlet weak private var paperSwitch2: RAMPaperSwitch!
@IBOutlet weak private var paperSwitch2: PaperSwitch!



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setupPaperSwitch()


self.navigationController?.navigationBarHidden = true
}



private func setupPaperSwitch() {

self.paperSwitch1.animationDidStartClosure = {(onAnimation: Bool) in

self.animateLabel(self.connectContactsLabel, onAnimation: onAnimation, duration: self.paperSwitch1.duration)
self.animateImageView(self.phone1ImageView, onAnimation: onAnimation, duration: self.paperSwitch1.duration)
paperSwitch1.animationStartBlock = { (animation:CAAnimation!) in
print("111111111111")
self.animateLabel(self.connectContactsLabel, onAnimation: true, duration: animation.duration)
self.animateImageView(self.phone1ImageView, onAnimation: true, duration: animation.duration)

}


self.paperSwitch2.animationDidStartClosure = {(onAnimation: Bool) in
paperSwitch2.animationStartBlock = { (animation:CAAnimation!) in
print("2222222222222")
self.animateLabel(self.allowDiscoveryLabel, onAnimation: true, duration: animation.duration)
self.animateImageView(self.phone2ImageView, onAnimation: true, duration: animation.duration)

self.animateLabel(self.self.allowDiscoveryLabel, onAnimation: onAnimation, duration: self.paperSwitch2.duration)
self.animateImageView(self.phone2ImageView, onAnimation: onAnimation, duration: self.paperSwitch2.duration)
}

// self.paperSwitch1.animationDidStartClosure = {(onAnimation: Bool) in
//
// self.animateLabel(self.connectContactsLabel, onAnimation: onAnimation, duration: self.paperSwitch1.duration)
// self.animateImageView(self.phone1ImageView, onAnimation: onAnimation, duration: self.paperSwitch1.duration)
// }
//
//
// self.paperSwitch2.animationDidStartClosure = {(onAnimation: Bool) in
//
// self.animateLabel(self.self.allowDiscoveryLabel, onAnimation: onAnimation, duration: self.paperSwitch2.duration)
// self.animateImageView(self.phone2ImageView, onAnimation: onAnimation, duration: self.paperSwitch2.duration)
// }
}


Expand Down
6 changes: 3 additions & 3 deletions PaperSwitchDemo/PaperSwitchDemo/ViewController.xib
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9531" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
Expand Down Expand Up @@ -67,7 +67,7 @@ added to your friends list.</string>
<constraint firstAttribute="width" constant="23" id="TxM-0R-JP4"/>
</constraints>
</imageView>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="r8u-XF-soy" customClass="RAMPaperSwitch" customModule="PaperSwitchDemo" customModuleProvider="target">
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="r8u-XF-soy" customClass="PaperSwitch">
<rect key="frame" x="200" y="152" width="51" height="31"/>
<constraints>
<constraint firstAttribute="height" constant="31" id="B6j-At-6lo"/>
Expand Down Expand Up @@ -125,7 +125,7 @@ added to your friends list.</string>
<constraint firstAttribute="width" constant="39" id="vUL-Ad-yFb"/>
</constraints>
</imageView>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="Lkc-kV-pLw" customClass="RAMPaperSwitch" customModule="PaperSwitchDemo" customModuleProvider="target">
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="Lkc-kV-pLw" customClass="PaperSwitch">
<rect key="frame" x="200" y="152" width="51" height="31"/>
<constraints>
<constraint firstAttribute="height" constant="31" id="m5s-RP-HIQ"/>
Expand Down