Skip to content

Commit 1f2c2a1

Browse files
committed
optimized blur effect
1 parent ac25264 commit 1f2c2a1

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

TYAlertControllerDemo/Base.lproj/Main.storyboard

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="15A284" 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="9059" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
33
<dependencies>
44
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9049"/>
66
</dependencies>
77
<scenes>
88
<!--View Controller-->
@@ -48,6 +48,9 @@
4848
<exclude reference="HNK-ek-Ch4"/>
4949
</mask>
5050
</variation>
51+
<connections>
52+
<action selector="costomAlertViewAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="t7s-6Y-ycr"/>
53+
</connections>
5154
</button>
5255
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vCS-SU-E0R">
5356
<rect key="frame" x="222" y="100" width="156" height="30"/>

TYAlertControllerDemo/TYAlertController/TYAlertController.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,17 @@ - (void)setBackgroundView:(UIView *)backgroundView
138138
if (_backgroundView == nil) {
139139
_backgroundView = backgroundView;
140140
} else if (_backgroundView != backgroundView) {
141-
[_backgroundView removeFromSuperview];
142-
_backgroundView = backgroundView;
143-
[self addBackgroundView];
144-
[self addSingleTapGesture];
141+
backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
142+
[self.view insertSubview:backgroundView aboveSubview:_backgroundView];
143+
[self.view addConstraintToView:backgroundView edageInset:UIEdgeInsetsZero];
144+
backgroundView.alpha = 0;
145+
[UIView animateWithDuration:0.3 animations:^{
146+
backgroundView.alpha = 1;
147+
} completion:^(BOOL finished) {
148+
[_backgroundView removeFromSuperview];
149+
_backgroundView = backgroundView;
150+
[self addSingleTapGesture];
151+
}];
145152
}
146153
}
147154

TYAlertControllerDemo/ViewController.m

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#import "UIImage+ImageEffects.h"
1212

1313
@interface ViewController ()
14-
1514
@end
1615

1716
@implementation ViewController
@@ -56,18 +55,18 @@ - (IBAction)showActionSheetAction:(id)sender {
5655
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"A message should be a short, complete sentence."];
5756

5857
[alertView addAction:[TYAlertAction actionWithTitle:@"默认2" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
59-
58+
NSLog(@"%@",action.title);
6059
}]];
6160

6261
[alertView addAction:[TYAlertAction actionWithTitle:@"默认1" style:TYAlertActionStyleDefault handler:^(TYAlertAction *action) {
63-
62+
NSLog(@"%@",action.title);
6463
}]];
6564

6665
[alertView addAction:[TYAlertAction actionWithTitle:@"删除" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
67-
66+
NSLog(@"%@",action.title);
6867
}]];
6968
[alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
70-
69+
NSLog(@"%@",action.title);
7170
}]];
7271

7372
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleActionSheet];
@@ -78,20 +77,37 @@ - (IBAction)blurEffectAlertViewAction:(id)sender {
7877
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"This is a blur effect on background, is beautiful effect"];
7978

8079
[alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
81-
80+
NSLog(@"%@",action.title);
8281
}]];
8382

8483
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];
8584

86-
UIImage *blurImage =[[UIImage snapshotImageWithView:self.view] applyLightEffect];
87-
UIImageView *blurImageView = [[UIImageView alloc]initWithImage:blurImage];
88-
alertController.backgroundView = blurImageView;
85+
// time consuming task ,so use dispatch_async .很耗时的操作
86+
dispatch_async(dispatch_get_global_queue(0, 0), ^{
87+
// 处理耗时操作的代码块...
88+
UIImage *blurImage =[[UIImage snapshotImageWithView:self.view] applyLightEffect];
89+
//通知主线程刷新
90+
dispatch_async(dispatch_get_main_queue(), ^{
91+
UIImageView *blurImageView = [[UIImageView alloc]initWithImage:blurImage];
92+
alertController.backgroundView = blurImageView;
93+
});
94+
95+
});
96+
97+
// UIImage *blurImage =[[UIImage snapshotImageWithView:self.view] applyLightEffect];
98+
// UIImageView *blurImageView = [[UIImageView alloc]initWithImage:blurImage];
99+
// alertController.backgroundView = blurImageView;
89100

90101
//alertController.alertViewOriginY = 60;
91102
[self presentViewController:alertController animated:YES completion:nil];
92103
}
93104

94105

106+
- (IBAction)costomAlertViewAction:(id)sender {
107+
108+
}
109+
110+
95111
- (IBAction)showAlertViewInWindowAction:(id)sender {
96112

97113
// UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

0 commit comments

Comments
 (0)