Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Add action block #19

Open
wants to merge 1 commit 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
10 changes: 10 additions & 0 deletions QBPopupMenu/QBPopupMenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

@property (nonatomic, weak, readonly) id target;
@property (nonatomic, assign, readonly) SEL action;
@property (nonatomic, copy, readonly) void(^actionBlock)();

@property (nonatomic, copy, readonly) NSString *title;
@property (nonatomic, copy, readonly) UIImage *image;
Expand All @@ -24,4 +25,13 @@
- (instancetype)initWithImage:(UIImage *)image target:(id)target action:(SEL)action;
- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image target:(id)target action:(SEL)action;

+ (instancetype)itemWithTitle:(NSString *)title actionBlcok: (void(^)())action;
+ (instancetype)itemWithImage:(UIImage *)image actionBlock: (void(^)())action;
+ (instancetype)itemWithTitle:(NSString *)title image:(UIImage *)image actionBlcok: (void(^)())action;

- (instancetype)initWithTitle:(NSString *)title actionBlock: (void(^)())action;
- (instancetype)initWithImage:(UIImage *)image actionBlock: (void(^)())action;
- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image actionBlcok: (void(^)())action;


@end
35 changes: 35 additions & 0 deletions QBPopupMenu/QBPopupMenuItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ @interface QBPopupMenuItem ()

@property (nonatomic, weak, readwrite) id target;
@property (nonatomic, assign, readwrite) SEL action;
@property (nonatomic, copy, readwrite) void(^actionBlock)();

@property (nonatomic, copy, readwrite) NSString *title;
@property (nonatomic, copy, readwrite) UIImage *image;
Expand Down Expand Up @@ -60,4 +61,38 @@ - (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image target:(i
return self;
}


+ (instancetype)itemWithTitle:(NSString *)title actionBlcok: (void(^)())action
{
return [[self alloc] initWithTitle:title actionBlock:action];
}
+ (instancetype)itemWithImage:(UIImage *)image actionBlock: (void(^)())action
{
return [[self alloc] initWithImage:image actionBlock:action];
}
+ (instancetype)itemWithTitle:(NSString *)title image:(UIImage *)image actionBlcok: (void(^)())action
{
return [[self alloc] initWithTitle:title image:image actionBlcok:action];
}

- (instancetype)initWithTitle:(NSString *)title actionBlock: (void(^)())action
{
return [self initWithTitle:title image:nil actionBlcok:action];
}
- (instancetype)initWithImage:(UIImage *)image actionBlock: (void(^)())action
{
return [self initWithTitle:nil image:image actionBlcok:action];
}
- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image actionBlcok: (void(^)())action
{
self = [super init];
if (self) {
self.title = title;
self.image = image;
self.actionBlock = action;
}
return self;
}


@end
3 changes: 3 additions & 0 deletions QBPopupMenu/QBPopupMenuItemView.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ - (void)setItem:(QBPopupMenuItem *)item

- (void)performAction
{
if (self.item.actionBlock) {
self.item.actionBlock();
}
if (self.item.target && self.item.action) {
[self.item.target performSelector:self.item.action withObject:nil afterDelay:0];
}
Expand Down
62 changes: 45 additions & 17 deletions QBPopupMenuDemo/QBPopupMenuDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4514" systemVersion="13A603" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3747"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -18,38 +20,64 @@
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="background.jpg" translatesAutoresizingMaskIntoConstraints="NO" id="SVa-hM-Tci">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ikL-ji-9TX">
<rect key="frame" x="99" y="184" width="122" height="33"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="QZq-OA-HI4">
<rect key="frame" x="73" y="165" width="175" height="33"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Light" family="Helvetica Neue" pointSize="17"/>
<state key="normal" title="QBPopupMenu">
<state key="normal" title="QBPlasticPopupMenu">
<color key="titleColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="showPopupMenu:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="4VF-zd-5Tc"/>
<action selector="showPlasticPopupMenu:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="LNF-6L-y6z"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="JGI-bE-Mei">
<rect key="frame" x="88" y="230" width="145" height="30"/>
<state key="normal" title="QBPopupMenuBlcok">
<color key="titleColor" red="0.20000000000000001" green="0.20000000000000001" blue="0.20000000000000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="showPopupMenuBlock:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="hp8-Wu-jQr"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="QZq-OA-HI4">
<rect key="frame" x="73" y="350" width="175" height="33"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="n0x-i3-mzO">
<rect key="frame" x="65" y="268" width="191" height="30"/>
<state key="normal" title="QBPlasticPopupMenuBlcok">
<color key="titleColor" red="0.20000000000000001" green="0.20000000000000001" blue="0.20000000000000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="showPlasticPopupMenuBlcok:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="D8f-sz-MEE"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ikL-ji-9TX">
<rect key="frame" x="99" y="124" width="122" height="33"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Light" family="Helvetica Neue" pointSize="17"/>
<state key="normal" title="QBPlasticPopupMenu">
<state key="normal" title="QBPopupMenu">
<color key="titleColor" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="showPlasticPopupMenu:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="LNF-6L-y6z"/>
<action selector="showPopupMenu:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="4VF-zd-5Tc"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="SVa-hM-Tci" firstAttribute="top" secondItem="kh9-bI-dsS" secondAttribute="top" id="95y-cr-F4Z"/>
<constraint firstItem="SVa-hM-Tci" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" id="QVs-Ys-DG2"/>
<constraint firstAttribute="trailing" secondItem="SVa-hM-Tci" secondAttribute="trailing" id="cJB-t6-fAU"/>
<constraint firstAttribute="centerX" secondItem="JGI-bE-Mei" secondAttribute="centerX" constant="-0.5" id="2v4-Ot-B5m"/>
<constraint firstItem="SVa-hM-Tci" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leadingMargin" constant="-16" id="90w-27-29t"/>
<constraint firstItem="SVa-hM-Tci" firstAttribute="top" secondItem="8x1-Wx-gRy" secondAttribute="bottom" constant="-20" id="9co-E0-wmk"/>
<constraint firstAttribute="centerY" secondItem="n0x-i3-mzO" secondAttribute="centerY" constant="1" id="P6T-nU-LGh"/>
<constraint firstItem="6dO-9s-DUx" firstAttribute="top" secondItem="SVa-hM-Tci" secondAttribute="bottom" id="UEK-Uc-OLk"/>
<constraint firstAttribute="centerY" secondItem="QZq-OA-HI4" secondAttribute="centerY" constant="102.5" id="a0M-dy-GIO"/>
<constraint firstAttribute="centerX" secondItem="QZq-OA-HI4" secondAttribute="centerX" constant="-0.5" id="ceT-Ft-Eo3"/>
<constraint firstAttribute="centerX" secondItem="n0x-i3-mzO" secondAttribute="centerX" constant="-0.5" id="dqV-AV-2LQ"/>
<constraint firstAttribute="centerY" secondItem="ikL-ji-9TX" secondAttribute="centerY" constant="143.5" id="eU1-8h-pKK"/>
<constraint firstAttribute="trailingMargin" secondItem="SVa-hM-Tci" secondAttribute="trailing" constant="-16" id="haW-TR-eXA"/>
<constraint firstAttribute="centerY" secondItem="JGI-bE-Mei" secondAttribute="centerY" constant="39" id="uTx-FY-v2j"/>
<constraint firstAttribute="centerX" secondItem="ikL-ji-9TX" secondAttribute="centerX" id="zg0-6r-POg"/>
<constraint firstItem="6dO-9s-DUx" firstAttribute="top" secondItem="SVa-hM-Tci" secondAttribute="bottom" id="zxV-gD-D7n"/>
</constraints>
</view>
Expand All @@ -67,4 +95,4 @@
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>
</document>
2 changes: 0 additions & 2 deletions QBPopupMenuDemo/QBPopupMenuDemo/QBPopupMenuDemo-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
43 changes: 43 additions & 0 deletions QBPopupMenuDemo/QBPopupMenuDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ @interface ViewController ()
@property (nonatomic, strong) QBPopupMenu *popupMenu;
@property (nonatomic, strong) QBPlasticPopupMenu *plasticPopupMenu;

@property (nonatomic, strong) QBPopupMenu *popupMenuBlcok;
@property (nonatomic, strong) QBPlasticPopupMenu *plasticPopupMenuBlcok;

@end

@implementation ViewController
Expand All @@ -39,6 +42,37 @@ - (void)viewDidLoad
QBPlasticPopupMenu *plasticPopupMenu = [[QBPlasticPopupMenu alloc] initWithItems:items];
plasticPopupMenu.height = 40;
self.plasticPopupMenu = plasticPopupMenu;


QBPopupMenuItem *bItem = [QBPopupMenuItem itemWithTitle:@"Hello" actionBlcok:^{
NSLog(@"Hello");
}];
QBPopupMenuItem *bItem2 = [QBPopupMenuItem itemWithTitle:@"Cut" actionBlcok:^{
NSLog(@"Cut");
}];
QBPopupMenuItem *bItem3 = [QBPopupMenuItem itemWithTitle:@"Copy" actionBlcok:^{
NSLog(@"Copy");
}];
QBPopupMenuItem *bItem4 = [QBPopupMenuItem itemWithTitle:@"Delete" actionBlcok:^{
NSLog(@"Delete");
}];
QBPopupMenuItem *bItem5 = [QBPopupMenuItem itemWithImage:[UIImage imageNamed:@"clip"] actionBlock:^{
NSLog(@"Clip Image");
}];
QBPopupMenuItem *bItem6 = [QBPopupMenuItem itemWithTitle:@"Delete" image:[UIImage imageNamed:@"trash"] actionBlcok:^{
NSLog(@"Trash Image");
}];
NSArray *bItems = @[bItem, bItem2, bItem3, bItem4, bItem5, bItem6];

QBPopupMenu *popupMenuBlcok = [[QBPopupMenu alloc] initWithItems:bItems];
popupMenuBlcok.highlightedColor = [[UIColor colorWithRed:0 green:0.478 blue:1.0 alpha:1.0] colorWithAlphaComponent:0.8];
self.popupMenuBlcok = popupMenuBlcok;

QBPlasticPopupMenu *plasticPopupMenuBlcok = [[QBPlasticPopupMenu alloc] initWithItems:bItems];
plasticPopupMenuBlcok.height = 40;
self.plasticPopupMenuBlcok = plasticPopupMenuBlcok;


}


Expand All @@ -53,6 +87,15 @@ - (IBAction)showPlasticPopupMenu:(id)sender
UIButton *button = (UIButton *)sender;
[self.plasticPopupMenu showInView:self.view targetRect:button.frame animated:YES];
}
- (IBAction)showPopupMenuBlock:(id)sender {
UIButton *button = (UIButton *)sender;
[self.popupMenuBlcok showInView:self.view targetRect:button.frame animated:YES];
}
- (IBAction)showPlasticPopupMenuBlcok:(id)sender {
UIButton *button = (UIButton *)sender;
[self.plasticPopupMenuBlcok showInView:self.view targetRect:button.frame animated:YES];

}

- (void)action
{
Expand Down