Core Animation實戰三(圖層幾何學)
錨點
瞭解遊戲的人一般都知道錨點,在UIView中我們很少用到。
anchorPoint
位於圖層的中點,所以圖層的將會以這個點為中心放置。anchorPoint
屬性並沒有被UIView
介面暴露出來,這也是檢視的position屬性被叫做“center”的原因。但是圖層的anchorPoint
可以被移動,比如你可以把它置於圖層frame
的左上角,於是圖層的內容將會向右下角的position
方向移動(圖3.3),而不是居中了。Demo:
主要是看下指標的位置
Xib指標位置如圖(錨點在中心位置):
最後效果如下:
Demo程式碼:
//
// ClockViewController.m
// LayerStudyDemo
//
// Created by apple on 2017/9/25.
// Copyright © 2017年 ZY. All rights reserved.
//
#import "ClockViewController.h"
@interface ClockViewController ()
@property (weak, nonatomic) IBOutlet UILabel *hourLabel;
@property (weak, nonatomic) IBOutlet UILabel *minuteLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;
@property (nonatomic, weak) NSTimer *timer;
@end
@implementation ClockViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(showTime) userInfo:nil repeats:YES];
}
-(void)showTime{
NSCalendar * calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierChinese];
NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
CGFloat hoursAngle = (components.hour / 12.0) * M_PI * 2.0;
//calculate hour hand angle //calculate minute hand angle
CGFloat minsAngle = (components.minute / 60.0) * M_PI * 2.0;
//calculate second hand angle
CGFloat secsAngle = (components.second / 60.0) * M_PI * 2.0;
//設定錨點
self.hourLabel.layer.anchorPoint =self.minuteLabel.layer.anchorPoint =self.secondLabel.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
//rotate hands
self.hourLabel.transform = CGAffineTransformMakeRotation(hoursAngle);
self.minuteLabel.transform = CGAffineTransformMakeRotation(minsAngle);
self.secondLabel.transform = CGAffineTransformMakeRotation(secsAngle);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
座標系和Hit Testing
CALayer
並不關心任何響應鏈事件,所以不能直接處理觸控事件或者手勢。但是它有一系列的方法幫你處理事件:-containsPoint:
和-hitTest:
。
-containsPoint:
接受一個在本圖層座標系下的CGPoint
,如果這個點在圖層frame
範圍內就返回YES
。
-hitTest:
方法同樣接受一個CGPoint
型別引數,而不是BOOL
型別,它返回圖層本身,或者包含這個座標點的葉子節點圖層。這意味著不再需要像使用-containsPoint:
那樣,人工地在每個子圖層變換或者測試點選的座標。如果這個點在最外面圖層的範圍之外,則返回nil。
//
// HitTestingViewController.m
// LayerStudyDemo
//
// Created by apple on 2017/9/25.
// Copyright © 2017年 ZY. All rights reserved.
//
#import "HitTestingViewController.h"
@interface HitTestingViewController ()
@property (weak, nonatomic) IBOutlet UIView *wildView;
@property (nonatomic, strong) CALayer *innerLayer;
@end
@implementation HitTestingViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatInnerLayer];
}
-(void)creatInnerLayer{
self.innerLayer = [CALayer layer];
self.innerLayer.frame = CGRectMake((self.wildView.frame.size.width-100)/2, (self.wildView.frame.size.height-100)/2, 100.0f, 100.0f);
self.innerLayer.backgroundColor = [UIColor blueColor].CGColor;
//add it to our view
[self.wildView.layer addSublayer:self.innerLayer];
}
// containsPoint 判斷較麻煩,需要把座標轉換圖層成每個座標系下的座標
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [[touches anyObject] locationInView:self.view];
//將點選的點從以 self.view.layer 為父類的座標系轉到self.wildView.layer為父類的座標系
point = [self.wildView.layer convertPoint:point fromLayer:self.view.layer];
if ([self.wildView.layer containsPoint:point]) {
//convert point to blueLayer’s coordinates
point = [self.innerLayer convertPoint:point fromLayer:self.wildView.layer];
if ([self.innerLayer containsPoint:point]) {
[[[UIAlertView alloc] initWithTitle:@"Inside innerLayer Layer"
message:nil
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
} else {
[[[UIAlertView alloc] initWithTitle:@"Inside wildView Layer"
message:nil
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
}
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//
// CGPoint point = [[touches anyObject] locationInView:self.view];
// //返回點選layer
// CALayer * hitLayer = [self.wildView.layer hitTest:point];
// if (hitLayer==self.innerLayer) {
//
// [[[UIAlertView alloc] initWithTitle:@"Inside innerLayer Layer"
// message:nil
// delegate:nil
// cancelButtonTitle:@"OK"
// otherButtonTitles:nil] show];
// } else if(hitLayer==self.wildView.layer){
// [[[UIAlertView alloc] initWithTitle:@"Inside wildView Layer"
// message:nil
// delegate:nil
// cancelButtonTitle:@"OK"
// otherButtonTitles:nil] show];
// }else{
// [[[UIAlertView alloc] initWithTitle:@"Inside View"
// message:nil
// delegate:nil
// cancelButtonTitle:@"OK"
// otherButtonTitles:nil] show];
// }
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相關文章
- 視覺效果 -- iOS Core Animation 系列三視覺iOS
- Core Animation總結
- Core Animation 之 ViewView
- 【IDL】幾何圖形數學運算函式函式
- 核心動畫(Core Animation Programming)動畫
- Core Animation Programming Guide Mind MappingGUIIDEAPP
- three.js 幾何體(三)JS
- geogebra幾何畫圖工具用法
- 【asp.net core 系列】8 實戰之 利用 EF Core 完成資料操作層的實現ASP.NET
- Web 前端實戰(三):雷達圖Web前端
- 論 DevOps 實踐有幾何?dev
- opencv 圖片幾何變換-縮放OpenCV
- .Net Core2.2 + EF Core + DI,三層框架專案搭建教程框架
- Flutter開發實戰分析-animation_demo解析導讀Flutter
- 數學趣題:平面幾何(一)
- 數學趣題:平面幾何(二)
- 【學習筆記】計算幾何筆記
- 三維幾何生成:多段線、圓弧
- WPF 反射載入Geometry幾何圖形資料圖示反射
- .Net Core後端架構實戰【1-專案分層框架設計】後端架構框架
- 系統學習iOS動畫之三:圖層動畫iOS動畫
- Python垃圾回收(GC)三層心法,你瞭解到第幾層?PythonGC
- java core dump分析實戰Java
- 深度學習和幾何(演講提要)深度學習
- 微分幾何學習(一)(向量函式)函式
- 平面幾何
- abp(net core)+easyui+efcore倉儲系統——領域層建立實體(三)UI
- 用幾張圖實戰講解MySQL主從複製MySql
- 玩轉iOS開發:7.《Core Animation》Implicit AnimationsiOS
- WPF繪圖(一):幾何(Geometry)與形狀(Shape)繪圖
- 不可不知的WPF幾何圖形(Geometry)
- 黃崢退了,拼多多的新戰鬥力幾何?
- 精益化的三個層次,你們做到了第幾層?
- 入學管理系統的頂層圖和一層圖
- 梯度下降背後的數學原理幾何?梯度
- js 實現 bind 的這五層,你在第幾層?JS
- Android基礎夯實–重溫動畫(三)之初識Property AnimationAndroid動畫
- .net core下訪問控制層的實現
- iOS開發UI篇--iOS動畫(Core Animation)總結iOSUI動畫