Skip to content

Commit f523814

Browse files
committed
1.0
模仿微信自定义actionsheet
1 parent d53adc9 commit f523814

File tree

6 files changed

+220
-9
lines changed

6 files changed

+220
-9
lines changed

CCActionSheet.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
AA21592A1C5B40A400F4076C /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA2159281C5B40A400F4076C /* Main.storyboard */; };
1414
AA21592C1C5B40A400F4076C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AA21592B1C5B40A400F4076C /* Assets.xcassets */; };
1515
AA21592F1C5B40A400F4076C /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA21592D1C5B40A400F4076C /* LaunchScreen.storyboard */; };
16+
AA2159381C5B410500F4076C /* CCActionSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = AA2159371C5B410500F4076C /* CCActionSheet.m */; };
1617
/* End PBXBuildFile section */
1718

1819
/* Begin PBXFileReference section */
@@ -26,6 +27,8 @@
2627
AA21592B1C5B40A400F4076C /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
2728
AA21592E1C5B40A400F4076C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
2829
AA2159301C5B40A400F4076C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
30+
AA2159361C5B410500F4076C /* CCActionSheet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCActionSheet.h; sourceTree = "<group>"; };
31+
AA2159371C5B410500F4076C /* CCActionSheet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCActionSheet.m; sourceTree = "<group>"; };
2932
/* End PBXFileReference section */
3033

3134
/* Begin PBXFrameworksBuildPhase section */
@@ -67,6 +70,8 @@
6770
AA21592D1C5B40A400F4076C /* LaunchScreen.storyboard */,
6871
AA2159301C5B40A400F4076C /* Info.plist */,
6972
AA21591F1C5B40A400F4076C /* Supporting Files */,
73+
AA2159361C5B410500F4076C /* CCActionSheet.h */,
74+
AA2159371C5B410500F4076C /* CCActionSheet.m */,
7075
);
7176
path = CCActionSheet;
7277
sourceTree = "<group>";
@@ -152,6 +157,7 @@
152157
AA2159271C5B40A400F4076C /* ViewController.m in Sources */,
153158
AA2159241C5B40A400F4076C /* AppDelegate.m in Sources */,
154159
AA2159211C5B40A400F4076C /* main.m in Sources */,
160+
AA2159381C5B410500F4076C /* CCActionSheet.m in Sources */,
155161
);
156162
runOnlyForDeploymentPostprocessing = 0;
157163
};
@@ -262,6 +268,7 @@
262268
buildSettings = {
263269
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
264270
INFOPLIST_FILE = CCActionSheet/Info.plist;
271+
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
265272
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
266273
PRODUCT_BUNDLE_IDENTIFIER = com.megahealth.MPlus.CCActionSheet;
267274
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -273,6 +280,7 @@
273280
buildSettings = {
274281
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
275282
INFOPLIST_FILE = CCActionSheet/Info.plist;
283+
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
276284
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
277285
PRODUCT_BUNDLE_IDENTIFIER = com.megahealth.MPlus.CCActionSheet;
278286
PRODUCT_NAME = "$(TARGET_NAME)";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
</Bucket>
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="BYZ-38-t0r">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
56
</dependencies>
67
<scenes>
78
<!--View Controller-->
89
<scene sceneID="tne-QT-ifu">
910
<objects>
10-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="" sceneMemberID="viewController">
11-
<layoutGuides>
12-
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
13-
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
14-
</layoutGuides>
11+
<viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
1512
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
16-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
13+
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
1714
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
15+
<subviews>
16+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="Ygh-eI-OBs">
17+
<rect key="frame" x="108" y="164" width="83" height="30"/>
18+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
19+
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
20+
<state key="normal" title="actionSheet"/>
21+
<connections>
22+
<action selector="showCCActionSheet:" destination="BYZ-38-t0r" eventType="touchUpInside" id="lNF-6j-XkB"/>
23+
</connections>
24+
</button>
25+
</subviews>
1826
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
1927
</view>
2028
</viewController>
2129
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
2230
</objects>
31+
<point key="canvasLocation" x="162" y="306"/>
2332
</scene>
2433
</scenes>
2534
</document>

CCActionSheet/CCActionSheet.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// CCActionSheet.h
3+
// CCActionSheet
4+
//
5+
// Created by maxmoo on 16/1/29.
6+
// Copyright © 2016年 maxmoo. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@protocol CCActionSheetDelegate <NSObject>
12+
13+
@optional
14+
- (void)cc_actionSheetDidSelectedIndex:(NSInteger)index;
15+
16+
@end
17+
18+
19+
20+
@interface CCActionSheet : UIView
21+
22+
@property (strong, nonatomic) id<CCActionSheetDelegate> delegate;
23+
24+
+ (instancetype)shareSheet;
25+
/**
26+
区分取消和选择,使用array
27+
回调使用协议
28+
*/
29+
- (void)cc_actionSheetWithSelectArray:(NSArray *)array cancelTitle:(NSString *)cancel;
30+
31+
32+
33+
@end

CCActionSheet/CCActionSheet.m

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//
2+
// CCActionSheet.m
3+
// CCActionSheet
4+
//
5+
// Created by maxmoo on 16/1/29.
6+
// Copyright © 2016年 maxmoo. All rights reserved.
7+
//
8+
9+
#import "CCActionSheet.h"
10+
11+
#define CC_SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
12+
#define CC_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
13+
14+
#define cellHeight 45
15+
16+
@interface CCActionSheet()
17+
18+
@property (nonatomic, strong) UIWindow *sheetWindow;
19+
@property (nonatomic, strong) NSArray *selectArray;
20+
@property (nonatomic, strong) NSString *cancelString;
21+
@property (nonatomic, strong) UIView *sheetView;
22+
@property (nonatomic, strong) UIView *backView;
23+
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
24+
25+
@end
26+
27+
@implementation CCActionSheet
28+
29+
+ (instancetype)shareSheet{
30+
static id shareSheet;
31+
static dispatch_once_t once;
32+
dispatch_once(&once, ^{
33+
shareSheet = [[[self class] alloc] init];
34+
});
35+
return shareSheet;
36+
}
37+
38+
- (void)cc_actionSheetWithSelectArray:(NSArray *)array cancelTitle:(NSString *)cancel{
39+
40+
self.selectArray = [NSArray arrayWithArray:array];
41+
self.cancelString = cancel;
42+
43+
if (!_sheetWindow) {
44+
[self initSheetWindow];
45+
}
46+
_sheetWindow.hidden = NO;
47+
48+
[self showSheetWithAnimation];
49+
}
50+
51+
- (void)initSheetWindow{
52+
_sheetWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, CC_SCREEN_WIDTH, CC_SCREEN_HEIGHT)];
53+
_sheetWindow.windowLevel = UIWindowLevelStatusBar;
54+
_sheetWindow.backgroundColor = [UIColor clearColor];
55+
56+
_sheetWindow.hidden = YES;
57+
58+
//zhezhao
59+
_backView = [[UIView alloc] initWithFrame:_sheetWindow.bounds];
60+
_backView.backgroundColor = [UIColor blackColor];
61+
_backView.alpha = 0.0;
62+
[_sheetWindow addSubview:_backView];
63+
64+
_tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(SingleTap:)];
65+
_tapGesture.numberOfTapsRequired = 1;
66+
[_backView addGestureRecognizer:_tapGesture];
67+
68+
UIView *selectView = [self creatSelectButton];
69+
70+
[_sheetWindow addSubview:selectView];
71+
}
72+
73+
74+
- (void)showSheetWithAnimation{
75+
CGFloat viewHeight = cellHeight * (self.selectArray.count+1) + 5 + (self.selectArray.count - 2) * 2;
76+
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
77+
_sheetView.frame = CGRectMake(0, CC_SCREEN_HEIGHT - viewHeight, CC_SCREEN_WIDTH, viewHeight);
78+
_backView.alpha = 0.2;
79+
} completion:^(BOOL finished) {
80+
81+
}];
82+
}
83+
84+
- (void)hidSheetWithAnimation{
85+
CGFloat viewHeight = cellHeight * (self.selectArray.count+1) + 5 + (self.selectArray.count - 2) * 2;
86+
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
87+
_sheetView.frame = CGRectMake(0, CC_SCREEN_HEIGHT, CC_SCREEN_WIDTH, viewHeight);
88+
_backView.alpha = 0.0;
89+
} completion:^(BOOL finished) {
90+
[self hidActionSheet];
91+
}];
92+
}
93+
94+
- (UIView *)creatSelectButton{
95+
CGFloat viewHeight = cellHeight * (self.selectArray.count+1) + 5 + (self.selectArray.count - 2) * 2;
96+
_sheetView = [[UIView alloc] initWithFrame:CGRectMake(0, CC_SCREEN_HEIGHT, CC_SCREEN_WIDTH, viewHeight)];
97+
_sheetView.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
98+
99+
for (int i = 0; i < self.selectArray.count; i++) {
100+
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
101+
button.frame = CGRectMake(0, i * (cellHeight+1), CC_SCREEN_WIDTH, cellHeight);
102+
[button setTitle:[NSString stringWithFormat:@"%@",self.selectArray[i]] forState:UIControlStateNormal];
103+
[button setBackgroundColor:[UIColor whiteColor]];
104+
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
105+
[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
106+
[button addTarget:self action:@selector(buttonSelectAction:) forControlEvents:UIControlEventTouchUpInside];
107+
button.tag = 1001+i;
108+
[_sheetView addSubview:button];
109+
}
110+
111+
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
112+
cancelButton.frame = CGRectMake(0, viewHeight - cellHeight, CC_SCREEN_WIDTH, cellHeight);
113+
cancelButton.backgroundColor = [UIColor whiteColor];
114+
[cancelButton setTitle:[NSString stringWithFormat:@"%@",self.cancelString] forState:UIControlStateNormal];
115+
[cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
116+
[cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
117+
[cancelButton addTarget:self action:@selector(buttonSelectAction:) forControlEvents:UIControlEventTouchUpInside];
118+
cancelButton.tag = 1000;
119+
[_sheetView addSubview:cancelButton];
120+
121+
return _sheetView;
122+
}
123+
124+
- (void)buttonSelectAction:(UIButton *)btn{
125+
UIButton *button = (UIButton *)btn;
126+
NSInteger index = button.tag - 1000;
127+
if (self.delegate && [self.delegate respondsToSelector:@selector(cc_actionSheetDidSelectedIndex:)]) {
128+
[self.delegate cc_actionSheetDidSelectedIndex:index];
129+
}
130+
[self hidSheetWithAnimation];
131+
}
132+
133+
-(void)SingleTap:(UITapGestureRecognizer*)recognizer
134+
{
135+
[self hidSheetWithAnimation];
136+
}
137+
138+
- (void)hidActionSheet{
139+
_sheetWindow.hidden = YES;
140+
_sheetWindow = nil;
141+
}
142+
143+
@end

CCActionSheet/ViewController.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
//
88

99
#import "ViewController.h"
10+
#import "CCActionSheet.h"
1011

11-
@interface ViewController ()
12+
13+
@interface ViewController ()<CCActionSheetDelegate>
14+
- (IBAction)showCCActionSheet:(UIButton *)sender;
1215

1316
@end
1417

@@ -24,4 +27,14 @@ - (void)didReceiveMemoryWarning {
2427
// Dispose of any resources that can be recreated.
2528
}
2629

30+
- (IBAction)showCCActionSheet:(UIButton *)sender {
31+
NSArray *array = @[@"小视频",@"拍照",@"从手机相册选择"];
32+
[[CCActionSheet shareSheet]cc_actionSheetWithSelectArray:array cancelTitle:@"取消"];
33+
[CCActionSheet shareSheet].delegate = self;
34+
}
35+
36+
- (void)cc_actionSheetDidSelectedIndex:(NSInteger)index{
37+
NSLog(@"selected index:%ld",(long)index);
38+
}
39+
2740
@end

0 commit comments

Comments
 (0)