Swift – UIAlertController

iOS 8 之后苹果出了一个UIAlertController,这个使用起来会比原来的UIAlertView 更加灵活一些,同时也可以集成上拉菜单。可以任意添加textfield,并且通过block来更方便的实现内部方法。
下面写一下简单集成,首先创建一个UIAlertController:
[code lang=”Objective-C”]
let alertController = UIAlertController(title: “标题”, message: “这个是UIAlertController的默认样式”, preferredStyle: UIAlertControllerStyle.Alert)
// 添加两个TextField
alertController.addTextFieldWithConfigurationHandler {
(textField: UITextField!) -> Void in
textField.placeholder = “登录”
// 在此可以添加一个textField的监听,对里面输入的内容可以进行分辨
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“alertTextFieldDidChange:”), name: UITextFieldTextDidChangeNotification, object: textField)
}
alertController.addTextFieldWithConfigurationHandler {
(textField: UITextField!) -> Void in
textField.placeholder = “密码”
textField.secureTextEntry = true
}
// 取消按钮
let cancelAction = UIAlertAction(title: “取消”, style: UIAlertActionStyle.Cancel, handler: nil)
alertController.addAction(cancelAction)
// 普通按钮(在此可以直接添加按钮点击的响应事件)
let okAction = UIAlertAction(title: “好的”, style: UIAlertActionStyle.Default) {
(action: UIAlertAction!) -> Void in
let login = (alertController.textFields?.first)! as UITextField
let password = (alertController.textFields?.last)! as UITextField
print(“\(login.text)—\(password.text)”)
// 上一个添加的textField监听可以在此移除
NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextFieldTextDidChangeNotification, object: nil)
}

alertController.addAction(okAction)
// 警告按钮
let resetAction = UIAlertAction(title: “警告”, style: UIAlertActionStyle.Destructive, handler: nil)
alertController.addAction(resetAction)

presentViewController(alertController, animated: true, completion: nil)
然后需要实现textField的监听方法

func alertTextFieldDidChange(notification: NSNotification){
let alertController = presentedViewController as! UIAlertController?
if (alertController != nil) {
let login = (alertController!.textFields?.first)! as UITextField
let okAction = alertController!.actions.last! as UIAlertAction
// 读取字符串长度(1汉字=3长度)
print("\(login.text?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))")
// 读取字符串长度(1汉字=1长度)
print("\(login.text?.characters.count)")
}
}
[/code]
接下来写下上拉菜单的集成:
[code lang="Objective-C"]
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .ActionSheet)

let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
let deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: nil)
let archiveAction = UIAlertAction(title: "保存", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(cancelAction)
alert.addAction(deleteAction)
alert.addAction(archiveAction)

self.presentViewController(alert, animated: true, completion: nil)

// 此代码为iPad上运行时所需要,在iPad上不需要写取消按钮,点击其他区域默认取消
let popover = alert.popoverPresentationController
if (popover != nil){
popover?.sourceView = sender
popover?.sourceRect = sender.bounds
popover?.permittedArrowDirections = UIPopoverArrowDirection.Any
}[/code]

Swift 获取iPhone设备相关信息

获取iPhone机器名称:UIDevice.currentDevice().name
获取设备当前版本:UIDevice.currentDevice().systemVersion
获取设备当前系统名称:UIDevice.currentDevice().systemName
获取设备当前系统名称:
UIDevice.currentDevice().identifierForVendor?.UUIDString
获取设备运营商名称:
import CoreTelephony
CTTelephonyNetworkInfo().subscriberCellularProvider?.carrierName
也可以使用:
CTTelephonyNetworkInfo().currentRadioAccessTechnology
是设备震动:
import AudioToolbox
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
获取设备电池电量:
UIDevice.currentDevice().batteryMonitoringEnabled = true
// 需要注意在iOS8 之前只能精确到0.05 iOS8之后可以精确到0.01
UIDevice.currentDevice().batteryLevel

iOS 3D Touch 这个技能不白学

随着iOS9和6S的到来,3D Touch已经被人们逐渐熟悉和使用,所以本篇主要讲讲这个新技能如何get,以及如何做的更出色。

首先可以先判断该设备是否支持3D touch:
[code lang=”Objective-C”]if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
// UIViewControllerPreviewingDelegate
[self registerForPreviewingWithDelegate:self sourceView:_imageView];
}[/code]
然后实现delegate:
[code lang=”Objective-C”]- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    UIButton *imageView = (UIButton *)[previewingContext sourceView];
    [previewingContext setSourceRect:imageView.bounds];

    POPViewController *p = [[POPViewController alloc] init];
    p.preferredContentSize = CGSizeMake(0, 100);
    p.view.backgroundColor = [UIColor blackColor];
    return p;
}[/code]
[code lang=”Objective-C”]- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self presentViewController:viewControllerToCommit animated:YES completion:nil];
}[/code]
这样可以简单完成一个3D touch Demo,想要实现上拉展示文字可以这样做:
[code lang=”Objective-C”]- (NSArray *)preActions
{
    if (!_preActions) {   
    UIPreviewAction *action0 = [UIPreviewAction actionWithTitle:@”test” style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
    }];

    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@”test1″ style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

    }];

    UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@”actionGroup” style:UIPreviewActionStyleSelected actions:@[action0, action1]];

    _preActions = @[action0, action1, actionGroup];
    }

    return _preActions;
}
[/code]
[code lang=”Objective-C”]
// 设置控制器在弹窗时候,下面输出的数组- (NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
    return self.preActions;
}[/code]
以上就是粗略的实现了内部3D touch 的功能

WebView只需要添加
[code lang=”Objective-C”]webView.allowsLinkPreview = YES[/code]
即可实现3D touch 的功能
[code lang=”Objective-C”]UITouch *touch = touches.anyObject;[/code]
touch.force这个可以看到用的按压的力度做出相应的判断