有没有办法检测设备的平面和横向或平面和纵向?
我问的是,因为我-(NSMutableString *)retrieveErrorHistory:(char *)ipToConn:(NSString *)port:(int)errHistSock:(BOOL)isEventHistory {
struct sockaddr_in broadcastAddr;
unsigned int broadcastPort = [pacPort intValue];
char recvString[1057];
socklen_t broadcastAddrLen = sizeof(broadcastAddr);
int bytes_received;
recvString[1056] = '\0'; // Put a null terminator on the end of the string...
memset(&broadcastAddr, 0, sizeof(broadcastAddr));
broadcastAddr.sin_family = AF_INET;
broadcastAddr.sin_addr.s_addr = inet_addr(ipToConn);
broadcastAddr.sin_port = htons(broadcastPort);
retrievedHistory = NO; // reset flag...
[self startHistoryTimeoutMonitor];
if ((bytes_received = recvfrom(errHistSock, recvString, 1057, 0, (struct sockaddr *)&broadcastAddr, &broadcastAddrLen)) < 0) {
NSLog(@"retrieveErrorHistory: recFrom Failed");
[self closeHistorySocket];
return nil;
}
retrievedHistory = YES; // got the history...
BOOL wasHistory = NO;
int rowBegin = 18;
int COL1_BEGIN = 0;
int COL1_LENGTH = 7;
int COL2_BEGIN = 8;
int COL2_LENGTH = 8;
int COL3_BEGIN = 17;
// The timestamp on the event history is not the same length...
if (isEventHistory) {
COL2_LENGTH = 5;
COL3_BEGIN = 14;
}
for (int totalRows = 20; totalRows > 0; totalRows--) {
NSString *row = [[NSString alloc] initWithFormat:@"%s", &recvString[rowBegin]];
NSString *trimmedRow = [row stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([trimmedRow length] >= 15) { // 15 character is the size of the date/time... it should be at least that long...
[allHist appendString: @"<tr style=\"vertical-align: top;\">"];
[allHist appendString: @"<td style=\"border-left: Solid 1px #000000; border-right: Solid 1px #000000;font-size:34px;"];
[allHist appendString: @"border-bottom: Solid 1px #000000; width: 15%; padding-right: 10px;"];
[allHist appendString: @"padding-left: 10px; vertical-align: center;\"><p style=\"\""];
[allHist appendString: @"align=\"left\">"];
[allHist appendString: [trimmedRow substringWithRange: NSMakeRange (COL1_BEGIN, COL1_LENGTH)]];
[allHist appendString: @"</p></td>"];
[allHist appendString: @"<td style=\"border-left: Solid 1px #000000; border-right: Solid 1px #000000;font-size:34px;"];
[allHist appendString: @"border-bottom: Solid 1px #000000; width: 15%; padding-right: 10px;"];
[allHist appendString: @"padding-left: 10px; vertical-align: center;\"><p style=\"\""];
[allHist appendString: @"align=\"left\">"];
[allHist appendString: [trimmedRow substringWithRange: NSMakeRange (COL2_BEGIN, COL2_LENGTH)]];
[allHist appendString: @"</p></td>"];
[allHist appendString: @"<td style=\"border-left: Solid 1px #000000; border-right: Solid 1px #000000;font-size:34px;"];
[allHist appendString: @"border-bottom: Solid 1px #000000; width: 40%; padding-right: 10px;"];
[allHist appendString: @"padding-left: 10px; vertical-align: center;\"><p style=\"\""];
[allHist appendString: @"align=\"left\">"];
[allHist appendString: [trimmedRow substringFromIndex: COL3_BEGIN]];
[allHist appendString: @"</p></td></tr>"];
wasHistory = YES;
rowBegin = rowBegin + 52; // 52 is the spacing between each row in the message
}
else {
break;
}
[row release];
//[trimmedRow release]; //Crashes when I release it....??
}
if (!wasHistory) {
[allHist appendString: @"<tr style=\"vertical-align: top;\">"];
[allHist appendString: @"<td colspan=\"3\" style=\"border-left: Solid 1px #000000; border-right: Solid 1px #000000;font-size:34px;"];
[allHist appendString: @"border-bottom: Solid 1px #000000; width: 15%; padding-right: 10px;"];
[allHist appendString: @"padding-left: 10px; vertical-align: center;\"><p style=\"\""];
[allHist appendString: @"align=\"left\">"];
[allHist appendString: @"No History to Display\n"];
[allHist appendString: @"</p></td></tr>"];
}
[allHist appendString: @"</table>"];
[allHist appendString: @"<p> </p>"];
return allHist;
}
UICollectionView
并且在纵向模式下,单元格排成一行。在景观中,它们只是在列中 - 它们从水平转向垂直。我每60秒更新一次收集数据。在这段时间之后必须调用UIViewController
来实现数据,如果我将设备*保持在平面方向,布局将被销毁,因为reloadData()
。没有任何条件是完美的。
sizeForItemAt
没有办法像这样放置它:
let isPortrait = UIDevice.current.orientation.isPortrait
if !isPortrait {
return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height / 4)
} else {
return CGSize(width: collectionView.bounds.width / 4, height: collectionView.bounds.height)
}
不能同时let isFlat = UIDevice.currentorientation.isFlat
if isFlat && isPortrait {
//
}
和isPortrait
。
这有什么工作吗?
答案 0 :(得分:0)
在我看来,要解决它,你应该保存最后一个项目的大小。当我们的设备保持平坦时,sizeForItemAt
将返回项目的最后一个尺寸。
var currentSize : CGSize?
override func viewDidLoad() {
super.viewDidLoad()
// In viewDidLoad, need to set default value for currentSize. Make sure in case
// your app start at flat orientation, you collectionView still has a size
currentSize = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height / 4);
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if UIDevice.current.orientation.isPortrait {
currentSize = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height / 4)
} else if UIDevice.current.orientation.isPortrait {
currentSize = CGSize(width: collectionView.bounds.width / 4, height: collectionView.bounds.height)
}
return currentSize;
}
当您的设备处于纵向或横向模式时,仅更新currentSize
的值。如果没有,请使用项目的当前大小。
答案 1 :(得分:0)
在iOS 12
中,您可以直接从Apple SDK获得新的定位:
.isFlat
“一个布尔值,指示指定的方向是否为面部 向上或面朝下。”
UIDeviceOrientation.isFlat
https://developer.apple.com/documentation/uikit/uideviceorientation/2981866-isflat